|
hi,
i am trying to access data through named pipes. Data is written in the named pipe from a plugin(written in unmanaged C++);client pipe;. I wanted to make the whole application more easy to handle so I am trying to use C#;server pipe; to take in the data from the named pipe for further processing.
My problem is I can get the client and server connected, but the C# code has hard time reading the data from the named pipe. The c++ code writes the data in the named pipe in LPTSTR format.
the data is basically real-time data from a gaming application.
Any suggestions on how to make the data accessible from the c# code, or on using any other medium besides named pipes to communicate the data between the two application will be highly appreciated.
thanks
letiny
|
|
|
|
|
Are you saying you are putting unmanaged text pointers into the pipe?
if so I guess you will need some kind of Marshal.PtrToString method, something I hope will never happen to me. I tend to get all objects created by managed code, and then pass their pointers to the primitive side for production/consumption.
Luc Pattyn [Forum Guidelines] [My Articles]
The quality and detail of your question reflects on the effectiveness of the help you are likely to get.
Show formatted code inside PRE tags, and give clear symptoms when describing a problem.
|
|
|
|
|
the plugin uses WriteFile() function to write in the named pipe. The WriteFile() function takes in the data param in LPTSTR format.
i am using sr.ReadLine() method in the C# code where sr is as follows.
i.e.;
NamedPipeServerStream pipeServer = new new NamedPipeServerStream(.....);
StreamReader sr = new StreamReader(pipeServer);
sr.ReadLine();
|
|
|
|
|
P.S.
I have already tried Marshal but could not find an option. I tried it again, when i use Marshal.PtrToStringUni(sr.ReadLine()), it gives me an error:
-->Error 2 The best overloaded method match for 'System.Runtime.InteropServices.Marshal.PtrToStringUni(System.IntPtr)' has some invalid arguments
-->Error 3 Argument '1': cannot convert from 'string' to 'System.IntPtr'
therefore I am guessing it is not a pointer.
The LPTSTR format is defined as zero-terminated string of TCHAR (TCHAR*) here[^]
|
|
|
|
|
Have you tried reading the bytes manually? ReadLine() returns a string, but Read() with a byte buffer might give you more to work with...
Chances are, if that works, you're going to see one of two things in the buffer:
1) A lot of data = Probably your raw data. You might have to strip out a trailing zero, then do an Encoding.ASCII.GetString()
2) Only a few bytes... That's your pointer... Extract the right number of bytes (4 for a 32-bit system, 8 for a 64-bit - Can probably get that from the framework)... Turn it into an IntPtr and try marshalling it (Or is that unmarshalling?)
Either way, get the data in a buffer so you know what you're dealing with.
|
|
|
|
|
Exactly, look at the raw data, and try and recognize it.
Luc Pattyn [Forum Guidelines] [My Articles]
The quality and detail of your question reflects on the effectiveness of the help you are likely to get.
Show formatted code inside PRE tags, and give clear symptoms when describing a problem.
|
|
|
|
|
This looks promising
I will definitely try this and let you know.
Thank you all of you guys for your suggestions!!
|
|
|
|
|
by this I meant the idea of using the Read() with a byte buffer.
Thanks again.
|
|
|
|
|
|
I have already gone through these articles you send me.
I was successfully able to create named pipe in c# both client and server and run them.
I was also able to do the same with visual C++ code.
The game plugin is written in visual c++, it outputs some real-time game data that I need. I have written named pipe client in visual c++ within the plugin which writes the data to the named pipe.
My problem is I have to process these data a lot for my final output, so it would make my life easier if I could access them through c# code. So I have programmed the server in C#
I can connect the client and server but c# code is not understanding the data which is in LPTSTR format visual c++ uses (well at least that is my guess). I guessed this because I wrote a server in visual c++ which could successfully access the data from the plugin.
Do you have any suggestions???
letiny
|
|
|
|
|
akhanal wrote: c# code is not understanding the data which is in LPTSTR
I had to do something fairly similar recently. What I did was I wrote a managed C++ dll that reads the data from the named pipe, converts the unmanaged strings to managed strings and then passes the managed strings on to the C# code.
Why is common sense not common?
Never argue with an idiot. They will drag you down to their level where they are an expert.
Sometimes it takes a lot of work to be lazy
Individuality is fine, as long as we do it together - F. Burns
Help humanity, join the CodeProject grid computing team here
|
|
|
|
|
Hi all,
I'm trying to open a SQL Anywhere DB connection and setup a data reader with the data returned, only I keep getting the error:
'oConn.ServerVersion' threw an exception of type 'System.InvalidOperationException'
Here is my code:
string ConnString = "Data Source=***;UID=***;PWD=***";
SAConnection oConn = new SAConnection(ConnString);
oConn.Open();
string SQLStatement = "SELECT * FROM QAS.QAFISET WHERE MAINLVL='T' ORDER BY NAME";
SACommand command = new SACommand(SQLStatement, oConn);
SADataReader DRTest = command.ExecuteReader();
Any feedback would be greatful.
Thank you,
Mel
modified on Friday, July 24, 2009 2:25 PM
|
|
|
|
|
MelWheater wrote: string ConnString = "Data Source=xxx;UID=xxx;PWD=xxx";
There is porblem with your connection string. You didn't mention the Database name.
use this
string ConnString = "Data Source=ServerName;Initial catalog=Database;Name;UID=userid;PWD=password";
Hope this will help you
Abhijit Jana | Codeproject MVP
Web Site : abhijitjana.net
Don't forget to click "Good Answer" on the post(s) that helped you.
modified on Friday, July 24, 2009 2:41 PM
|
|
|
|
|
|
MelWheater wrote: Could you do me a give favor and remove the Data source UID and PWD from your message please. I forgot to take them out in my initial post
Done. I was also thinking about that when I saw you just changed it, but forgot . Now its done
yes, Let me know if you have any issue.
Abhijit Jana | Codeproject MVP
Web Site : abhijitjana.net
Don't forget to click "Good Answer" on the post(s) that helped you.
|
|
|
|
|
Thanks
I tried including the database name only I then receive an error from the second line:
SAConnection oConn = new SAConnection(ConnString);
The error reads:
Invalid connection string. Error parsing connection parameter string
Parameter name: connectionString
Do I have to supply the full path to the location of the database in the Data Source or would just the server name do? I've tried both but get the same error.
Mel
|
|
|
|
|
MelWheater wrote: The error reads:
Invalid connection string. Error parsing connection parameter string
Parameter name: connectionString
Did you tried with the solution I have provided to you ? Is that working ?
MelWheater wrote: Invalid connection string. Error parsing connection parameter string
Parameter name: connectionString
Error says that your connection string is not in proper format. please show us the connectiono string that you are using right now.
Note : Do not need to give servername, uid and password
Abhijit Jana | Codeproject MVP
Web Site : abhijitjana.net
Don't forget to click "Good Answer" on the post(s) that helped you.
|
|
|
|
|
Yes I'm using the Connection string that you suggested, which includes "Initial catalog" and that is the error I received (mentioned above).
The connection string:
string ConnString = "Data Source=SvrName;Initial catalog=DBName;UID=uid;PWD=pw";
|
|
|
|
|
Why you have deleted your post?
Abhijit Jana | Codeproject MVP
Web Site : abhijitjana.net
Don't forget to click "Good Answer" on the post(s) that helped you.
modified on Friday, July 24, 2009 2:57 PM
|
|
|
|
|
lol...because it wasn't relevant to the initial question.
|
|
|
|
|
Hi All,
I still haven't been able to SAConnection working correctly. I'm currently receiving the error:
"Invalid connection string. Error parsing connection parameter string
Parameter name: connectionString"
on the following line of code:
SAConnection oConn = new SAConnection(ConnString);
The connection string I'm using is of the following format:
string ConnString = "Data source=ServerName;Initial catalog=DBName;UID=UserID;PWD=Password";
Could you give me some pointers on what I should be entering for the 'Data Source' is this just the server name or the server name plus the path to the database etc? i've tried both with no luck but if I can pin point what is required then I have a better chance of getting it to work.
Hope you can help,
Mel
|
|
|
|
|
First of all when I select multiple rows to delete then it gives me an error:
"Uncomitted new rows cannot be deleted."
Secondly, if I select only one row at a time and click the delete button then it deletes all the rows from the database table.
[code]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Data.Sql;
using Dotnet67.Sales.DAL;
using Dotnet67.Sales.Items;
using Dotnet67.Sales.Persons;
using System.Data.SqlClient;
namespace Dotnet67.Sales.DAL
{
public class DALHelper
{
private readonly string CONSTRING3 = "CASHIER1";
private readonly string CONSTRING5 = "DELETE1";
// THE FOLLOWING IS THE CODE WHERE I GET ERRORS
public void DeleteUser(int id)
{
SqlTransaction tran = con.BeginTransaction();
SqlConnection con = new SqlConnection("server=.; Database=Dotnet67;uid=sa;pwd=123;");
SqlCommand com = new SqlCommand(CONSTRING5, con);
com.CommandType = CommandType.StoredProcedure;
com.Parameters.AddWithValue("@CashierID", id);
con.Open();
com.ExecuteNonQuery();
con.Close();
}
public void InsertIntoCashier(string[] str,int[] val)
{
SqlConnection con = new SqlConnection("server=.; Database=Dotnet67;uid=sa;pwd=123;");
SqlCommand com = new SqlCommand(this.CONSTRING3, con);
com.CommandType = CommandType.StoredProcedure;
com.Parameters.AddWithValue("@CashierID", val[0]);
com.Parameters.AddWithValue("@CashierName", str[0]);
con.Open();
try
{
com.ExecuteNonQuery();
}
finally
{
con.Close();
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Dotnet67.Sales.DAL;
using Dotnet67.CommonTypes;
using Dotnet67.WinControls;
using System.Data.Sql;
using System.Data.SqlClient;
namespace Dotnet67.Sales.WinUI
{
public partial class ManageCashier : Form
{
public ManageCashier()
{
InitializeComponent();
}
private readonly string CONSTRING5 = "DELETE1";
private void ManageCashier_Load(object sender, EventArgs e)
{
// TODO: This line of code loads data into the 'dotnet67DataSet8.Cashier_2' table. You can move, or remove it, as needed.
this.cashier_2TableAdapter.Fill(this.dotnet67DataSet8.Cashier_2);
}
//This is the button for inserting values into grid view which is running perfect.
private void btnDB_Click(object sender, EventArgs e)
{
DALHelper dH = new DALHelper();
string[] str = new string[3];
int[] values = new int[3];
str[0] = txtName.Text;
dH.InsertIntoCashier(str, values);
dataGridView1.Refresh();
dataGridView1.RefreshEdit();
this.cashier_2TableAdapter.Fill(this.dotnet67DataSet8.Cashier_2);
}
// THIS IS THE DELETE BUTTON ON MY FORM WHERE I AM GETTING PROBLEM
private void btnDELETE_Click(object sender, EventArgs e)
{
DALHelper dH = new DALHelper();
List<int> rowsToDelete = new List<int>();
for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
if (Convert.ToBoolean(dataGridView1["column1", i].Value))
{
rowsToDelete.Add(i);
}
foreach (int rowIndex in rowsToDelete)
{
dataGridView1.Rows.RemoveAt(rowIndex);
}
dH.DeleteUser(i);
}
}
}
}
[\code]
|
|
|
|
|
Omar Akhtar Sheikh wrote: First of all when I select multiple rows to delete then it gives me an error:
"Uncomitted new rows cannot be deleted."
It is becuase, you are trying to delete one row which is in Edit mode and not comitted with DB.
Omar Akhtar Sheikh wrote: dataGridView1.Rows.RemoveAt(rowIndex);
You just try to implement this.
if (!dataGridView1.Rows[i].IsNewRow)
Note : Please make sure, while post code , please post it with proper format. Otherwise it really make difficult to read and understand.
Abhijit Jana | Codeproject MVP
Web Site : abhijitjana.net
Don't forget to click "Good Answer" on the post(s) that helped you.
|
|
|
|
|
You have
SqlTransaction tran = con.BeginTransaction();
but no commit statement.... that may not be exactly the reason why your code fails but it is odd that you have BeginTransaction without CommitTransaction or Rollback.
Secondly, after doing edits to the datagrid, unless you save changes to database, you can't delete them from the database. You can only remove them from the data grid
|
|
|
|
|
I am trying to get the keystrokes as a user types but when my program loses focus (example is when minimized), i can no longer get the keys pressed. Any idea on how i can solve that problem?
Wamuti: Any man can be an island, but islands to need water around them!
Edmund Burke: No one could make a greater mistake than he who did nothing because he could do only a little.
|
|
|
|