C#如何调用c写成的dll。 主要是C函数包含Ref 参数。我的c#程序
调用方法一:
private static extern int Rcard(string comp,string dbuf);string RThree = “”;
int val2 = Rcard(GetCOMP(),RThree); 可以通过但是RThree 始终为空。 无法ref dbuf。调用方法二:private static extern int Rcard(string comp,ref string dbuf);string RThree = “”;
int val2 = Rcard(GetCOMP(),ref RThree); 程序报错“尝试读取或写入受保护的内存。这通常指示其他内存已损坏”请求帮忙,谢谢int PASCAL Rcard(LPSTR inb,LPSTR instr)
{
DCB dcb;
COMSTAT comstat;
HANDLE hd;
DWORD dwError;
DWORD dwBytes;
int i=0,retv=0;
long t1,t2;
unsigned char nb[]="COM2: baud=9600";
char setting[]="COM2: baud=9600 parity=O data=7 stop=1",comn[]="COM2";
unsigned char *ps; lstrcpy(nb,inb);
comn[3]=nb[3];
lstrcpy(setting,nb);
lstrcat(setting," parity=O data=7 stop=1"); hd=CreateFile( comn, GENERIC_READ | GENERIC_WRITE,
0, // exclusive access
NULL, // no security attrs
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL ,//|FILE_FLAG_OVERLAPPED, // overlapped I/O
NULL ); if(hd==INVALID_HANDLE_VALUE)
{ retv=-1; goto end; } if(GetCommState(hd,&dcb)!=TRUE)
{ retv=-3; goto end; } if(BuildCommDCB(setting,&dcb)!=TRUE)
{ retv=-2; goto end; } dcb.fRtsControl=RTS_CONTROL_ENABLE;
if(SetCommState(hd,&dcb)!=TRUE)
{ retv=-3; goto end; } ps=instr;
t1=GetTickCount();
t2=0;
GetAsyncKeyState(VK_ESCAPE); //clear the ESC pressed events
while(t2<=100)
{
if(GetAsyncKeyState(VK_ESCAPE)!=0)
{
retv=-5;
goto end;
}
ClearCommError(hd,&dwError,&comstat);
if(comstat.cbInQue!=0)
{
ReadFile(hd,ps,comstat.cbInQue,&dwBytes,NULL);
ps=ps+comstat.cbInQue;
t1=GetTickCount();
}
if(ps!=instr) t2=GetTickCount()-t1;
}
调用方法一:
private static extern int Rcard(string comp,string dbuf);string RThree = “”;
int val2 = Rcard(GetCOMP(),RThree); 可以通过但是RThree 始终为空。 无法ref dbuf。调用方法二:private static extern int Rcard(string comp,ref string dbuf);string RThree = “”;
int val2 = Rcard(GetCOMP(),ref RThree); 程序报错“尝试读取或写入受保护的内存。这通常指示其他内存已损坏”请求帮忙,谢谢int PASCAL Rcard(LPSTR inb,LPSTR instr)
{
DCB dcb;
COMSTAT comstat;
HANDLE hd;
DWORD dwError;
DWORD dwBytes;
int i=0,retv=0;
long t1,t2;
unsigned char nb[]="COM2: baud=9600";
char setting[]="COM2: baud=9600 parity=O data=7 stop=1",comn[]="COM2";
unsigned char *ps; lstrcpy(nb,inb);
comn[3]=nb[3];
lstrcpy(setting,nb);
lstrcat(setting," parity=O data=7 stop=1"); hd=CreateFile( comn, GENERIC_READ | GENERIC_WRITE,
0, // exclusive access
NULL, // no security attrs
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL ,//|FILE_FLAG_OVERLAPPED, // overlapped I/O
NULL ); if(hd==INVALID_HANDLE_VALUE)
{ retv=-1; goto end; } if(GetCommState(hd,&dcb)!=TRUE)
{ retv=-3; goto end; } if(BuildCommDCB(setting,&dcb)!=TRUE)
{ retv=-2; goto end; } dcb.fRtsControl=RTS_CONTROL_ENABLE;
if(SetCommState(hd,&dcb)!=TRUE)
{ retv=-3; goto end; } ps=instr;
t1=GetTickCount();
t2=0;
GetAsyncKeyState(VK_ESCAPE); //clear the ESC pressed events
while(t2<=100)
{
if(GetAsyncKeyState(VK_ESCAPE)!=0)
{
retv=-5;
goto end;
}
ClearCommError(hd,&dwError,&comstat);
if(comstat.cbInQue!=0)
{
ReadFile(hd,ps,comstat.cbInQue,&dwBytes,NULL);
ps=ps+comstat.cbInQue;
t1=GetTickCount();
}
if(ps!=instr) t2=GetTickCount()-t1;
}
可能性
或者(不安全代码)private static extern int Rcard(char* comp, char* dbuf);
在C中的String 都是指针或char[]
这个方法用C#也可以实现的
CharSet=CharSet.Ansi)]