Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

calling a DLL function from VB with ByRef arguments 1

Status
Not open for further replies.

IonelBurtan

Programmer
May 25, 2001
601
RO
I have build a MFC DLL with 2 exported functions like this:
#define EXPORTED extern __declspec(dllexport)
EXPORTED LRESULT SetEnviron(LPCSTR strEnviron)
EXPORTED long GetEnviron(LPSTR strEnviron)

In VB I have the 2 declarations:
public declare sub SetEnviron lib "parser.dll"(ByVal strEnviron as String)
public declare function GetEnviron lib "parser.dll"(ByRef strEnviron as String) as Long

The body of the functions is dummy so I don't include them here. The problem is the first function(a simple setter), works just fine, but the second one fails with the following simptoms:

1. When I run the executable that call the function GetEnviron from inside VB I get error no 49 "Bad Dll calling convention"
2. When I run the executable that call the function GetEnviron from outside VB I get an error like "The memory cold not be read" then the program crushes.
3. When I run the executabile by debuging the DLL from VC++ line by line,I see that my function executes fine, puts the result in the byref passed argument, then after the last line of the function(return 0L;) it crushes with error "OLEOUT32 Access Violation".


 
The standard calling convention is __stdcall. You should declare functions with this modificator. John Fill
1c.bmp


ivfmd@mail.md
 
I have made the following modification in my GetEnviron function

#define EXPORTED extern __declspec(dllexport) __stdcall
long EXPORTED FAR PASCAL GetEnviron(LPSTR FAR strEnviron,long* lSize)

in VB:
Public Declare Function GetEnviron(ByVal strEnviron As String, ByRef lSize As Long) As Long

It works very fine, thanks to your help, an advice from a friend in France and some of my ideeas.(Now my function resembles a call to the API function GetComputerName)

Thank you
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top