高手看过来:VB 调用 C++ dll问题
救救我,我对VB不熟悉啊。我在c++中输出函数如下:
m.def文件中
EXPORTS
fnDoMatch @1
m.h文件中
BOOL __stdcall fnDoMatch(const char *pszImgPathFileName, char *pszOutStr, int nOutStrLen);
生成的dll在VB中的调用的代码如下:
Private Declare Function fnDoMatch Lib "IdentifyDll.dll" (ByVal szImgFileName As String, ByRef szOut As String, ByVal OutBufLen As Integer) As Long
Private Sub Command1_Click()
Dim szFile As String
Dim szOut As String
Dim OutBufLen As Long
Dim I As Long
CommonDialog1.ShowOpen
szFile = CommonDialog1.FileName
szOut = Space(255)
I = fnDoMatch(szFile, szOut, OutBufLen)
MsgBox "i=" & I
MsgBox "szFile=" & szFile
MsgBox Left(szOut, OutBufLen)
End Sub
不知道为什么,调用总是失败,我在c++中的调用代码如下:
是没有问题的
我在VC的编译dll中
c/c++->code generation->struct member alignment-》已经选成4 bytes了
请问还有其他的需要调整吗?
HINSTANCE hs = LoadLibrary("m.dll");
if(hs)
{
try
{
pfnDoMatch = (fnDoMatch *)GetProcAddress((HINSTANCE)hs, "fnDoMatch");
if(pfnDoMatch)
{
char szOut[5] = {'\0'};
// fnDoMatch(g_szFileName, szOut, sizeof(szOut));
(*pfnDoMatch)(g_szFileName, szOut, sizeof(szOut));
TextOut(GetDC(hWnd), 10, 10, szOut, strlen(szOut));
}
}
catch (...)
{
}
FreeLibrary(hs);
}
请问为什么呢?