CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Apr 1999
    Location
    Rajasthan ,India
    Posts
    25

    Strange behaviour of DLL Calls in VB5

    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


  2. #2
    Join Date
    May 1999
    Posts
    3,332

    Re: Strange behaviour of DLL Calls in VB5

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


  3. #3
    Join Date
    Apr 1999
    Location
    Rajasthan ,India
    Posts
    25

    Re: Strange behaviour of DLL Calls in VB5

    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


  4. #4
    Join Date
    May 1999
    Posts
    3,332

    Re: Strange behaviour of DLL Calls in VB5

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


  5. #5
    Join Date
    Apr 1999
    Posts
    24

    Re: Strange behaviour of DLL Calls in VB5

    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)



  6. #6
    Join Date
    Apr 1999
    Location
    Rajasthan ,India
    Posts
    25

    Re: Strange behaviour of DLL Calls in VB5

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



Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured