Google 網路論壇不再支援新的 Usenet 貼文或訂閱項目,但過往內容仍可供查看。

Calling a C++ DLL which returns a String

瀏覽次數:63 次
跳到第一則未讀訊息

Vorreiter Johann (IFDA)

未讀,
2004年1月12日 凌晨4:20:002004/1/12
收件者:
Hi,

following problem:
in my EXCEL VBA-code I try to call a function from a C++ DLL which has the
following declaration:

long TestFunc( char* TestStr );

In this function the TestStr is modified.

VBA-code:

Declare Function TestFunc& lib "TestDll.dll" ( VBAString as String )

Dim DummyStr as String
DummyStr = "this is a test"
ret = TestFunc( DummyStr )

BUT, after calling TestFunc EXCEL crashes...
As long as I don't touch the string in the C++ funtion everything is ok.

Any other idea how to return a string from a DLL?


Thanks,

Johann


mct

未讀,
2004年1月12日 下午4:25:022004/1/12
收件者:
I don't know how your DLL was constructed; 'normal' Win32, as a Static
Library, or via MFC. But there is another way to import C-functions in
VB(A). This is how I get an Int.

DLL.DLL:

// DLL.cpp : Defines the entry point for the DLL application.
//

#include "stdafx.h"

UINT DLL_Function_Name();

BOOL APIENTRY DllMain( HANDLE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
return TRUE;
}

UINT DLL_Function_Name()
{
return 12345;
}


Then, DLL.DEF:

EXPORTS
DLL_Function_Name

Finally, in Excel VBA:

Option Explicit

Public Declare Function LoadLibrary Lib "kernel32" Alias "LoadLibraryA"
(ByVal lpLibFileName As String) As Long
Public Declare Function FreeLibrary Lib "kernel32" (ByVal hLibModule As
Long) As Long
Public Declare Function GetProcAddress Lib "kernel32" (ByVal hModule As
Long, ByVal lpProcName As String) As Long
Public Declare Function CallWindowProc Lib "user32" Alias "CallWindowProcA"
(ByVal lpPrevWndFunc As Long, ByVal hWnd As Long, ByVal Msg As Long, ByVal
wParam As Long, ByVal lParam As Long) As Long

Public Function DoDLLFunction() As Integer
Dim X As Long
Dim Y As Long
X = LoadLibrary("C:\Excel Projects Usenet\DLL2.DLL")
Y = GetProcAddress(X, "DLL_Function_Name")
MsgBox Y
DoDLLFunction = CallWindowProc(Y, ByVal 0&, ByVal 0&, ByVal 0&, ByVal
0&)
FreeLibrary X
End Function


...will give 12345 in the ActiveCell by entering =DoDLLFunction()
Maybe you can experiment with this.

hans


Hans


"Vorreiter Johann (IFDA)" <Johann.V...@infineon.com> schreef in bericht
news:bttos1$5cv$1...@newssrv.muc.infineon.com...

Vorreiter Johann (IFDA)

未讀,
2004年1月13日 凌晨1:59:182004/1/13
收件者:
Thanks Hans,

this works fine, but what to do if I want to return a string instead of an
UINT??

Regards,

Johann


"mct" <i_lov...@NOSPAMwanadoo.nl> wrote in message
news:400310ae$0$59451$1b62...@news.wanadoo.nl...

0 則新訊息