G
Guest
Hi,
I have an application which is using a dll written in C++.
When the program go to the function below, it would raise an
NullReferenceException at the line of PtrToStructure.
public bool ServerCallBack(IntPtr log)
{
DB_T acclog = (DB_T)Marshal.PtrToStructure(log, typeof(DB_T));
}
However, if I read the log IntPtr byte by Byte , it would return part of the
correct thing.
public bool ServerCallBack(IntPtr log)
{
byte[] record = new byte[100];
IntPtr dbPoint = log;
Marshal.Copy(dbPoint,record,0,99);
char [] CharArray=
System.Text.Encoding.ASCII.GetString(record).ToCharArray();
listBox1.Items.Add(CharArray[0]); <--print correct value
}
The structure of DB_T is like this:
[StructLayout(LayoutKind.Sequential)]
public struct DB_T
{
string a;
FILETIME t;
string b;
int c;
int d;
}
Can anyone please help me how can I marshal the IntPtr type to DB_T type in
C#?
I use similar ServerCallBack function , it works fine.
Thanks,
Grace
I have an application which is using a dll written in C++.
When the program go to the function below, it would raise an
NullReferenceException at the line of PtrToStructure.
public bool ServerCallBack(IntPtr log)
{
DB_T acclog = (DB_T)Marshal.PtrToStructure(log, typeof(DB_T));
}
However, if I read the log IntPtr byte by Byte , it would return part of the
correct thing.
public bool ServerCallBack(IntPtr log)
{
byte[] record = new byte[100];
IntPtr dbPoint = log;
Marshal.Copy(dbPoint,record,0,99);
char [] CharArray=
System.Text.Encoding.ASCII.GetString(record).ToCharArray();
listBox1.Items.Add(CharArray[0]); <--print correct value
}
The structure of DB_T is like this:
[StructLayout(LayoutKind.Sequential)]
public struct DB_T
{
string a;
FILETIME t;
string b;
int c;
int d;
}
Can anyone please help me how can I marshal the IntPtr type to DB_T type in
C#?
I use similar ServerCallBack function , it works fine.
Thanks,
Grace