本人C#小白。老大要求用C#調用C++的DLL.
C++的我寫出來了,可以調用成功.
#include
#include
void main(){
HMODULE hModule = ::LoadLibrary(_T("C:\CT_InfoTerm.dll"));
typedef LONG (WINAPI pDLLProc)(int,char,int,int,int,char*,int);
pDLLProc dllfunc;
int iPort = 0;
char cExtendPort[1] = {'9'};
int iBaudRate = 9600;
int iVoiceType = 0;
int iTimeOut = 30;
char cPin[256];
memset(cPin, NULL, sizeof(cPin));
dllfunc = (pDLLProc)
::GetProcAddress((HMODULE)hModule, "CT_ReadPin");
int iReturn;
if (dllfunc != NULL){
iReturn = dllfunc(iPort, cExtendPort, iBaudRate, iVoiceType, iTimeOut, cPin, sizeof(cPin));
}
}
現在改為C#的
[DllImport("CT_InfoTerm.dll", EntryPoint = "CT_ClosePinPad" ,CallingConvention=CallingConvention.Cdecl)]
public unsafe static extern int CT_ReadPin(int iPortNo, char extendPort, int iBaudRate, int iVoiceType, int iTimeOut, StringBuilder Pin, int iPinSize);
public unsafe void CT_ReadPin(WebCommand command)
{
int nRet = -3;//打開串口失敗(默認)
if (_canRead)
{
int iPortNo = 0;
char extendPort = '9';
int iBaudRate = 9600;
int iVoiceType = Convert.ToInt16(command.Parameter);
int iTimeOut =30;
//var Pin=new byte[100];
StringBuilder Pin = new StringBuilder(1024);
// string Pin = new string(Convert.ToChar(" "), 256);
// char Pin = new char();
int iPinSize =256;
nRet = Centerm.CT_ReadPin(iPortNo,extendPort,iBaudRate,iVoiceType,iTimeOut,Pin,iPinSize);
checkresult(command, nRet);
}
}
pin是返回值
注釋的地方是我嘗試的方法,並不報任何錯誤,但是C#的調用不成功,可能是pin這個參數有問題.
各位大神能不能提供一些思路,供我參考一下,多謝
pin是StringBuilder,要轉化成String類型啊