Calling a win32 DLL in a vb app - Need help 
Author Message
 Calling a win32 DLL in a vb app - Need help

Hello ,

  I have written a static DLL  which takes in an INTEGER and returns it 's Square value .also ineger . I   exported the function  'Calc'  by  using the keyword __declspec(dllexport) in the function's definition  . I have not  used  DEF files to export functions

My problem is this .. I checked the functionailty with an MFC and Console application in C and it worked fine . However when I tried the same in VB I got
a  'Bad  DLL Calling Convention ' Error .

Does this have something todo with the dll calling mechanism specific to VB ?  .

Here 's my code .Any help is most apprecaited .

J .

=====
Private Declare Function Calc Lib "D:\Projects\iSquare.dll"  (ByVal iSqr As Long) <--  here 's where the dll is called

Private Sub Command1_Click()
On Error GoTo errD:

    Dim iRes As Integer
    iRes = 0

    iRes = Calc(CInt(Text1))
    Text2 = CStr(iRes)

    Exit Sub

errD:
    MsgBox Err.Description
    Exit Sub

Resume

End Sub

=====



Sun, 27 Jun 2004 22:28:22 GMT  
 Calling a win32 DLL in a vb app - Need help
Visual Basic can only handle _stdcall methods.
For an example about this, go to http://www.allapi.net/vbtutor/vcdll1.php

Regards,
Pieter Philippaerts
http://www.allapi.net/


Hello ,

  I have written a static DLL  which takes in an INTEGER and returns it 's
Square value .also ineger . I   exported the function 'Calc'  by  using the
keyword __declspec(dllexport) in the function's definition  . I have not
used  DEF files to export functions

My problem is this .. I checked the functionailty with an MFC and Console
application in C and it worked fine . However when I tried the same in VB I
got
a  'Bad  DLL Calling Convention ' Error .

Does this have something todo with the dll calling mechanism specific to VB
?  .

Here 's my code .Any help is most apprecaited .

J .

=====
<SNIP>
=====



Sun, 27 Jun 2004 23:25:18 GMT  
 Calling a win32 DLL in a vb app - Need help

You have four problems:
1) You should either use a DEF file, or an Alias clause in your Declare statement with the munged name.
2) The function should be _stdcall, not _cdecl
3) Your Declare statement should return a Long, you're currently returning the default VB type of Variant. This in and of itself can give you a bad calling convention because the Variant return type is passed on the stack and Long is passed in registers, so the two functions have a different stack size.
4) You should never hard code a path in your Declare statements. I'll give you the benefit of the doubt and assume this is for testing purposes only, not for your final product.

Also, please tell us you're doing something other than squaring and integer in your Dll. You should just do this in VB. -Matt

  Hello ,

    I have written a static DLL  which takes in an INTEGER and returns it 's Square value .also ineger . I   exported the function 'Calc'  by  using the keyword __declspec(dllexport) in the function's definition  . I have not  used  DEF files to export functions

  My problem is this .. I checked the functionailty with an MFC and Console application in C and it worked fine . However when I tried the same in VB I got
  a  'Bad  DLL Calling Convention ' Error .

  Does this have something todo with the dll calling mechanism specific to VB ?  .

  Here 's my code .Any help is most apprecaited .

  J .

  =====
  Private Declare Function Calc Lib "D:\Projects\iSquare.dll"  (ByVal iSqr As Long) <--  here 's where the dll is called

  Private Sub Command1_Click()
  On Error GoTo errD:

      Dim iRes As Integer
      iRes = 0

      iRes = Calc(CInt(Text1))
      Text2 = CStr(iRes)

      Exit Sub

  errD:
      MsgBox Err.Description
      Exit Sub

  Resume

  End Sub

  =====



Mon, 28 Jun 2004 02:33:53 GMT  
 Calling a win32 DLL in a vb app - Need help

In fact, you can even square a value in VBScript.

cheers, jw

  You have four problems:
  1) You should either use a DEF file, or an Alias clause in your Declare statement with the munged name.
  2) The function should be _stdcall, not _cdecl
  3) Your Declare statement should return a Long, you're currently returning the default VB type of Variant. This in and of itself can give you a bad calling convention because the Variant return type is passed on the stack and Long is passed in registers, so the two functions have a different stack size.
  4) You should never hard code a path in your Declare statements. I'll give you the benefit of the doubt and assume this is for testing purposes only, not for your final product.

  Also, please tell us you're doing something other than squaring and integer in your Dll. You should just do this in VB. -Matt

    Hello ,

      I have written a static DLL  which takes in an INTEGER and returns it 's Square value .also ineger . I   exported the function 'Calc'  by  using the keyword __declspec(dllexport) in the function's definition  . I have not  used  DEF files to export functions

    My problem is this .. I checked the functionailty with an MFC and Console application in C and it worked fine . However when I tried the same in VB I got
    a  'Bad  DLL Calling Convention ' Error .

    Does this have something todo with the dll calling mechanism specific to VB ?  .

    Here 's my code .Any help is most apprecaited .

    J .

    =====
    Private Declare Function Calc Lib "D:\Projects\iSquare.dll"  (ByVal iSqr As Long) <--  here 's where the dll is called

    Private Sub Command1_Click()
    On Error GoTo errD:

        Dim iRes As Integer
        iRes = 0

        iRes = Calc(CInt(Text1))
        Text2 = CStr(iRes)

        Exit Sub

    errD:
        MsgBox Err.Description
        Exit Sub

    Resume

    End Sub

    =====



Mon, 28 Jun 2004 06:39:26 GMT  
 Calling a win32 DLL in a vb app - Need help

Guy 's  Thanks for the replies . But  the problem still exists  :-( .

Of course this is for testing purposes . This time I used a DEF file and  _stdcall convention with Aliases   ans specifying the reurn type as long,But still no luck . The error I got was ' Cant find DLL entry point x in x.dll '  .. Again I get this problem with  integers  .However this works in Debug mode .

This time I 'll attch my C code .  Maybe that could shed some light on the matter . I 've been wading thru MSDN all day with no success . :-(

BOOL APIENTRY DllMain( HANDLE hModule,
                       DWORD  ul_reason_for_call,
                       LPVOID lpReserved
      )
{

 switch (ul_reason_for_call)
 {
  case DLL_PROCESS_ATTACH:
  case DLL_THREAD_ATTACH:
  case DLL_THREAD_DETACH:
  case DLL_PROCESS_DETACH:
   break;
    }
    return TRUE;

Quote:
}

long _stdcall aa(int x)

{

 int y=0;
 y = x * x;
 return y;

Quote:
}


  You have four problems:
  1) You should either use a DEF file, or an Alias clause in your Declare statement with the munged name.
  2) The function should be _stdcall, not _cdecl
  3) Your Declare statement should return a Long, you're currently returning the default VB type of Variant. This in and of itself can give you a bad calling convention because the Variant return type is passed on the stack and Long is passed in registers, so the two functions have a different stack size.
  4) You should never hard code a path in your Declare statements. I'll give you the benefit of the doubt and assume this is for testing purposes only, not for your final product.

  Also, please tell us you're doing something other than squaring and integer in your Dll. You should just do this in VB. -Matt

    Hello ,

      I have written a static DLL  which takes in an INTEGER and returns it 's Square value .also ineger . I   exported the function 'Calc'  by  using the keyword __declspec(dllexport) in the function's definition  . I have not  used  DEF files to export functions

    My problem is this .. I checked the functionailty with an MFC and Console application in C and it worked fine . However when I tried the same in VB I got
    a  'Bad  DLL Calling Convention ' Error .

    Does this have something todo with the dll calling mechanism specific to VB ?  .

    Here 's my code .Any help is most apprecaited .

    J .

    =====
    Private Declare Function Calc Lib "D:\Projects\iSquare.dll"  (ByVal iSqr As Long) <--  here 's where the dll is called

    Private Sub Command1_Click()
    On Error GoTo errD:

        Dim iRes As Integer
        iRes = 0

        iRes = Calc(CInt(Text1))
        Text2 = CStr(iRes)

        Exit Sub

    errD:
        MsgBox Err.Description
        Exit Sub

    Resume

    End Sub

    =====



Mon, 28 Jun 2004 23:11:03 GMT  
 Calling a win32 DLL in a vb app - Need help
The declaration of your function should be

    int _stdcall aa(int x) {

and the VB declaration should be

    Private Declare Function aa Lib "iSquare.dll"  (ByVal iSqr As Long) as
Long

The C++ 'long' is not very compatible with VB.

If you then type 'aa' under 'EXPORTS' in your .DEF file,
everything should work properly.

Regards,
Pieter Philippaerts
http://www.allapi.net/


Guy 's  Thanks for the replies . But  the problem still exists  :-( .

Of course this is for testing purposes . This time I used a DEF file and
_stdcall convention with Aliases   ans specifying the reurn type as long,But
still no luck . The error I got was ' Cant find DLL entry point x in x.dll '
.. Again I get this problem with  integers  .However this works in Debug
mode .

This time I 'll attch my C code .  Maybe that could shed some light on the
matter . I 've been wading thru MSDN all day with no success . :-(

BOOL APIENTRY DllMain( HANDLE hModule,
                       DWORD  ul_reason_for_call,
                       LPVOID lpReserved
      )
{

 switch (ul_reason_for_call)
 {
  case DLL_PROCESS_ATTACH:
  case DLL_THREAD_ATTACH:
  case DLL_THREAD_DETACH:
  case DLL_PROCESS_DETACH:
   break;
    }
    return TRUE;

Quote:
}

long _stdcall aa(int x)

{

 int y=0;
 y = x * x;
 return y;

Quote:
}



Tue, 29 Jun 2004 00:42:57 GMT  
 Calling a win32 DLL in a vb app - Need help

'Link -dump -exports x.dll' will give you a list of exported functions. If the names still look mangled, then you didn't put enough information in the .DEF file. If you don't care, then use the 'Alias' clause of the declare statement to specify your entrypoint to be something other than the function name. -Matt

  Guy 's  Thanks for the replies . But  the problem still exists  :-( .

  Of course this is for testing purposes . This time I used a DEF file and  _stdcall convention with Aliases   ans specifying the reurn type as long,But still no luck . The error I got was ' Cant find DLL entry point x in x.dll '  .. Again I get this problem with  integers  .However this works in Debug mode .

  This time I 'll attch my C code .  Maybe that could shed some light on the matter . I 've been wading thru MSDN all day with no success . :-(

  BOOL APIENTRY DllMain( HANDLE hModule,
                         DWORD  ul_reason_for_call,
                         LPVOID lpReserved
        )
  {

   switch (ul_reason_for_call)
   {
    case DLL_PROCESS_ATTACH:
    case DLL_THREAD_ATTACH:
    case DLL_THREAD_DETACH:
    case DLL_PROCESS_DETACH:
     break;
      }
      return TRUE;
  }

  long _stdcall aa(int x)

  {

   int y=0;
   y = x * x;
   return y;
  }


    You have four problems:
    1) You should either use a DEF file, or an Alias clause in your Declare statement with the munged name.
    2) The function should be _stdcall, not _cdecl
    3) Your Declare statement should return a Long, you're currently returning the default VB type of Variant. This in and of itself can give you a bad calling convention because the Variant return type is passed on the stack and Long is passed in registers, so the two functions have a different stack size.
    4) You should never hard code a path in your Declare statements. I'll give you the benefit of the doubt and assume this is for testing purposes only, not for your final product.

    Also, please tell us you're doing something other than squaring and integer in your Dll. You should just do this in VB. -Matt

      Hello ,

        I have written a static DLL  which takes in an INTEGER and returns it 's Square value .also ineger . I   exported the function 'Calc'  by  using the keyword __declspec(dllexport) in the function's definition  . I have not  used  DEF files to export functions

      My problem is this .. I checked the functionailty with an MFC and Console application in C and it worked fine . However when I tried the same in VB I got
      a  'Bad  DLL Calling Convention ' Error .

      Does this have something todo with the dll calling mechanism specific to VB ?  .

      Here 's my code .Any help is most apprecaited .

      J .

      =====
      Private Declare Function Calc Lib "D:\Projects\iSquare.dll"  (ByVal iSqr As Long) <--  here 's where the dll is called

      Private Sub Command1_Click()
      On Error GoTo errD:

          Dim iRes As Integer
          iRes = 0

          iRes = Calc(CInt(Text1))
          Text2 = CStr(iRes)

          Exit Sub

      errD:
          MsgBox Err.Description
          Exit Sub

      Resume

      End Sub

      =====



Tue, 29 Jun 2004 08:31:04 GMT  
 Calling a win32 DLL in a vb app - Need help

Um, could you kindly stop posting in HTML?  TIA...

On to the question...

Quoth Josh Q:

Quote:
> Of course this is for testing purposes . This time I used a DEF file
> and  _stdcall convention with Aliases   ans specifying the reurn type
> as long,But still no luck . The error I got was ' Cant find DLL entry
> point x in x.dll '  .. Again I get this problem with  integers.
> However this works in Debug mode .
> long _stdcall aa(int x)

Looks OK.  If you want to be sure, try this:

    extern "C" int __stdcall aa(int x)
    // ...

Then, your .DEF file should look something like this:
    EXPORTS

Then, your VB declare:
  Private Declare Function aa Lib "mylib.dll" (ByVal x As Long) As Long

(where mylib.dll should be replaced with the appropriate name and path)



Wed, 30 Jun 2004 07:11:41 GMT  
 
 [ 8 post ] 

 Relevant Pages 

1. Need Help Calling Win32 Function from VB

2. Calling Win32 DLLs from VB 3.0

3. C++ Win32 DLL causes VB DX app to crash

4. Calling a function in a C++ Win32 Dll from VB 6

5. Need help calling 16-bit app from 32-bit app

6. Need HELP with Delphi DLL to be Called by VB 5.0

7. PLEASE HELP! VBA app calls in VB app

8. HELP: Need help with wininet.dll calls!

9. help with calling a vb.DLL

10. Help needed with using Win32 Net functions with VB

11. Win32 Checking for DLL/Windowless app?

 

 
Powered by phpBB® Forum Software