Click to See Complete Forum and Search --> : Strange behaviour of DLL Calls in VB5
Sanjeev
April 6th, 1999, 09:45 PM
Hi,
I've written a set of functions in MFC and I'm calling them in VB5 through a DLL.
If I make a exe file and run it separately, the app runs without any problems but if I run it within the VB Environment I get "Bad DLL calling convention (Error 49):" error!(& points to the 1st fn called from the DLL)
Can anybody Help me?
Thanx
Lothar Haensler
April 7th, 1999, 02:35 AM
MS DevNet says:
"Arguments passed to adynamic-link library (DLL) must exactly match those expected by the routine. Calling conventions deal with number, type, and order of arguments. This error has the following causes and solutions:
Your program is calling a routine in a DLL that's being passed the wrong type of arguments.
Make sure all argument types agree with those specified in the declaration of the routine you are calling.
Your program is calling a routine in a DLL that's being passed the wrong number of arguments.
Make sure you are passing the same number of arguments indicated in the declaration of the routine you are calling.
Your program is calling a routine in a DLL, but isn't using the StdCall calling convention.
If the DLL routine expects argumentsby value, then make sure ByVal is specified for those arguments in the declaration for the routine.
Your Declare statement for a Windows DLL includes CDecl. "
Sanjeev
April 8th, 1999, 02:46 AM
Hi,
Here are more details:
My DLL Fn written in MFC takes string(pointers to char) as the parameter, but I want to call the fn in VB5 by passing a string.
I've read that I can not pass VB string to a fn written in C as they don't match.
So how do I solve this problem
Thanx
Lothar Haensler
April 8th, 1999, 02:54 AM
you can certainly pass a string to a DLL function. That's what you do many times when calling User and kernel function from a VB program.
Excerpt from MSDevnet:
"NOTE: When you pass a string variable to an API call, you actually pass the memory address of the string, so you should always pass string parameters as ByVal. If you pass a string parameter by reference, you pass the memory address containing the memory address of the string, which causes the API function receiving the parameter to behave incorrectly and may cause a memory violation error. "
Looks like your declaration of the DLL function is invalid (missing ByVal... as string).
Dan O'Brien
April 9th, 1999, 07:56 AM
From what I understand, you may be right: A VB string (BSTR in VC++-speak) is a wide (UNICODE) string with its length prepended, while the C function would expect a pointer to either an ASCII string or a wide string (depending on how it was compiled) with no length prepended.
This aside, however, I've seen strings passed to C functions in DLLs. Try this:
Private Declare Function Foo Lib "foo32.dll" _(ByVal SomeString As String) As Integer
Dim i As Integer
i = Foo(SomeString)
Sanjeev
April 21st, 1999, 12:20 AM
Hi,
Thanx...
But I did try your suggestion....
Funny thing is ... if I compile and make EXE and run the prog it runs without any error....
but it gives the above error only when you run from the IDE.
Pl help me through this... my project is held-up due to this...
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.