Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2
Default Calling a C++ DLL which returns a String

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


  #2   Report Post  
Posted to microsoft.public.excel.programming
mct mct is offline
external usenet poster
 
Posts: 4
Default Calling a C++ DLL which returns a String

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)" schreef in bericht
...
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




  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2
Default Calling a C++ DLL which returns a String

Thanks Hans,

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

Regards,

Johann


"mct" wrote in message
...
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)" schreef in

bericht
...
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






Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
How do I return a text string when a calculation returns a non who bcronin Excel Worksheet Functions 9 August 6th 09 06:41 PM
Help formula that returns a text string from another sheet Stuart k Excel Worksheet Functions 1 April 7th 08 01:53 PM
A function that returns the formula of a cell as a string Lacty Excel Worksheet Functions 9 March 6th 08 05:13 PM
use concatenate function to put carrage returns in a text string dabblingandconfused Excel Worksheet Functions 4 August 15th 06 04:58 PM
calling variable within string [email protected] Excel Worksheet Functions 8 May 4th 06 03:07 PM


All times are GMT +1. The time now is 06:35 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"