A qustion on Dll Call
-
Hi everybody.I have problems when calling dll. Here is the Dll function definition(from the vander's document):
__int16 __stdcall MW_GetDBRecord(HANDLE icdev, __int16 fsid, __int16 rec_size, char *buf, int *rlen) Input parameters: icdev: device handle fsid: selected table NO. rec_size: a record size space. Output parameters : Buf: return entire record data (you need give buf enough space ); Rlen: length of data .
And here is my code:[DllImport("mwhandset")] public static extern Int16 MW_GetDBRecord(IntPtr icdev,Int16 fsid, Int16 rec_size,ref string buff,ref int rlen); ... private void button3_Click(object sender, System.EventArgs e) { ... int rlen = 0; string buff; buff = " ";//the length is enough MW_GetDBRecord(m_icdev,0,16,buff,ref rlen); }
When calling MW_GetDBRecord it raise "System.NullReferenceException: Object not set to an instance".I'm sure the function has completed on the device, because rlen=16(the record size) when the exception raised.So I think it is caused by type incompatibility,most likely the buff parameter.What type should I use corresponding to "char *buf"? Or it is caused by any other reason? Can anyone help me? Any idea will be appropriate. -
Hi everybody.I have problems when calling dll. Here is the Dll function definition(from the vander's document):
__int16 __stdcall MW_GetDBRecord(HANDLE icdev, __int16 fsid, __int16 rec_size, char *buf, int *rlen) Input parameters: icdev: device handle fsid: selected table NO. rec_size: a record size space. Output parameters : Buf: return entire record data (you need give buf enough space ); Rlen: length of data .
And here is my code:[DllImport("mwhandset")] public static extern Int16 MW_GetDBRecord(IntPtr icdev,Int16 fsid, Int16 rec_size,ref string buff,ref int rlen); ... private void button3_Click(object sender, System.EventArgs e) { ... int rlen = 0; string buff; buff = " ";//the length is enough MW_GetDBRecord(m_icdev,0,16,buff,ref rlen); }
When calling MW_GetDBRecord it raise "System.NullReferenceException: Object not set to an instance".I'm sure the function has completed on the device, because rlen=16(the record size) when the exception raised.So I think it is caused by type incompatibility,most likely the buff parameter.What type should I use corresponding to "char *buf"? Or it is caused by any other reason? Can anyone help me? Any idea will be appropriate.This has to be
Xiaoming Qian wrote:
[DllImport("mwhandset")] public static extern Int16 MW_GetDBRecord(IntPtr icdev,Int16 fsid, Int16 rec_size,ref string buff,ref int rlen);
Like this:
public static extern int MW_GetDBRecord(IntPtr icdev, int fsid, int rec_size, [Out] byte[] buf, int bufSize);
And this has to beXiaoming Qian wrote:
MW_GetDBRecord(m_icdev,0,16,buff,ref rlen);
this
int size = 256; //what ever you want it to be
byte[] buf = new byte[size];MW_GetDBRecord(m_icdev, 0, 16, buf, size);
I suggest you read these articles.[^]
Tarakeshwar Reddy MCP, CCIE Q(R&S) There are two kinds of people, those who do the work and those who take the credit. Try to be in the first group; there is less competition there. - Indira Gandhi
-
This has to be
Xiaoming Qian wrote:
[DllImport("mwhandset")] public static extern Int16 MW_GetDBRecord(IntPtr icdev,Int16 fsid, Int16 rec_size,ref string buff,ref int rlen);
Like this:
public static extern int MW_GetDBRecord(IntPtr icdev, int fsid, int rec_size, [Out] byte[] buf, int bufSize);
And this has to beXiaoming Qian wrote:
MW_GetDBRecord(m_icdev,0,16,buff,ref rlen);
this
int size = 256; //what ever you want it to be
byte[] buf = new byte[size];MW_GetDBRecord(m_icdev, 0, 16, buf, size);
I suggest you read these articles.[^]
Tarakeshwar Reddy MCP, CCIE Q(R&S) There are two kinds of people, those who do the work and those who take the credit. Try to be in the first group; there is less competition there. - Indira Gandhi
Thank you, it works. :-D