CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11
  1. #1
    Join Date
    Aug 2010
    Posts
    47

    Calling a DLL written in VC++.NET Native Code from a VB.NET project

    I have just installed VC++.net (2010 Express version) on a Win XP machine (the reason I am still using XP is that I need to use VB6 for a while and it will not install on Win 7/8). I would install 2013 VC++.net but it is only available for Win 7/8

    So, i have built a small test dll and am trying to call it from a project written in vb.net, as follows:

    In VC++
    Code:
    extern "C" {
    	__declspec(dllexport) double TestF(); 
    }
    
    __declspec(dllexport) double TestF(){
      
      double ax = 0; 
      ax = 999;
      return ax;
    }
    In vb.net
    Code:
    Public Declare Function TestF Lib "C:\....\mopeks.dll" () As Double
    
      dblX = TestF()
    This works and it returns "999" as hoped for. The problem is that when I include a parameter so that we have:

    __declspec(dllexport) double TestF(double dblValue){ etc etc

    I get PInvoke problems, namely:

    "PInvokeStackImbalance was detected
    Message: A call to PInvoke function 'MOPEKS!MOPEKS.prjDLLs::TestF' has unbalanced the stack. This is likely because the managed PInvoke signature does not match the unmanaged target signature. Check that the calling convention and parameters of the PInvoke signature match the target unmanaged signature."

    I suspect I need to use "_stdcall" or whatever? Any guidance much appreciated
    Last edited by wavering; August 6th, 2014 at 05:07 AM.
    MOPEKS - a freeware program that generates programs that use each other to solve problems. Is this the correct route to a genuinely intelligent machine?

  2. #2
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    4,626

    Re: Calling a DLL written in VC++.NET Native Code from a VB.NET project

    functions you export from a DLL which you intend to be usable from languages OTHER than C/C++ should be declared as being __stdcall (as opposed to the default __cdecl).

    That's for Win32 apps. For Win64 there's essentially only 1 supported calling convention for everything and that's the MS approach to __fastcall. Other compilers may support other conventions for internal code, but all interfaces to/from the outside of the app need to be according to the MS defined __fastcall.

  3. #3
    Join Date
    Aug 2010
    Posts
    47

    Re: Calling a DLL written in VC++.NET Native Code from a VB.NET project

    Quote Originally Posted by OReubens View Post
    functions you export from a DLL which you intend to be usable from languages OTHER than C/C++ should be declared as being __stdcall (as opposed to the default __cdecl).
    Yes, I have just tried that yet again as:

    __declspec(dllexport) double __stdcall TestF(double dblValue)

    and I get:

    Unable to find an entry point named 'TestF' in DLL 'C:\_MOPEKS\ ...\Debug\mopeks.dll'

    (the address is correct)

    Do I need to do anything in the VB.NET code - currently it is just

    Public Declare Function TestF Lib "C:\_MOPEKS\...\Debug\mopeks.dll" (dblX As Double) As Double

    Edit:
    Well folks, it turns out that you can do it like this - you add this gobbledegook to your vb.net project.

    <System.Runtime.InteropServices.DllImport("C:\_MOPEKS\ .... \Debug\mopeks.dll", SetLastError:=False)> Public Function TestF(dblX As Double) As Double
    End Function

    This works, thank the Lord. But surely it can be done more easily?

    [rant]
    Thank you Microsoft for making our lives so easy - no doubt when you take over the world we will all have to speak Chinese because it is a richer language
    [/rant]
    Last edited by wavering; August 7th, 2014 at 05:52 AM.
    MOPEKS - a freeware program that generates programs that use each other to solve problems. Is this the correct route to a genuinely intelligent machine?

  4. #4
    Join Date
    Jul 2002
    Posts
    2,543

    Re: Calling a DLL written in VC++.NET Native Code from a VB.NET project

    DllImport is correct way to call native function from VB .NET. Better to avoid full path: "C:\_MOPEKS\ ..." and leave only Dll name. Place Dll to .exe directory, current directory or make it available through PATH. Internally PInvoke calls LoadLibrary on native Dll, find in MSDN how LoadLibrary resolves library name.

  5. #5
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    4,626

    Re: Calling a DLL written in VC++.NET Native Code from a VB.NET project

    you probably forgot to also add the __stdcall in the extern-prototype definition.

    definition and declaration need to match up you know

  6. #6
    Join Date
    Aug 2014
    Posts
    6

    Re: Calling a DLL written in VC++.NET Native Code from a VB.NET project

    Quote Originally Posted by wavering View Post
    I have just installed VC++.net (2010 Express version) on a Win XP machine (the reason I am still using XP is that I need to use VB6 for a while and it will not install on Win 7/8). I would install 2013 VC++.net but it is only available for Win 7/8
    You can install and run the VB6 IDE on Windows 7 and 8.x.
    VB6 programming is fine under Windows 7/8 32 bit or 64 bit.

  7. #7
    Join Date
    Aug 2010
    Posts
    47

    Re: Calling a DLL written in VC++.NET Native Code from a VB.NET project

    Quote Originally Posted by VB6 Programming View Post
    You can install and run the VB6 IDE on Windows 7 and 8.x.
    VB6 programming is fine under Windows 7/8 32 bit or 64 bit.
    Well, I have tried to install it and failed. On reading around there seems to be a consensus that it cannot be done

    Do you need to use a virtual machine? Or what?
    MOPEKS - a freeware program that generates programs that use each other to solve problems. Is this the correct route to a genuinely intelligent machine?

  8. #8
    Join Date
    Aug 2014
    Posts
    6

    Re: Calling a DLL written in VC++.NET Native Code from a VB.NET project

    Quote Originally Posted by wavering View Post
    Well, I have tried to install it and failed. On reading around there seems to be a consensus that it cannot be done

    Do you need to use a virtual machine? Or what?
    No you don't need to use a VM.
    I've installed the VB6 IDE on Windows 7 and Windows 8.1 without any problem. Great for keeping VB6 programming current.

    Remember to install as an administrator (right click and 'Run as administrator'). Similarly when running the IDE run as administrator.
    I usually copy the VB6 CDs to disk first, setup a shortcut to the installer and install from there (but I have also installed direct from CD).

    I'm probably not the best person to advise you as I have never had a problem installing it.
    If you are still having difficulties, try switching off UAC.
    I installed from the Visual Studio 6.0 Enterprise Edition, others may be different (but Professional should install also).

    This may be useful
    http://www.vbforums.com/showthread.p...o-on-windows-8

    The link there posted by gibra may be useful. His VS6 installer is well regarded.

  9. #9
    Join Date
    Aug 2014
    Posts
    6

    Re: Calling a DLL written in VC++.NET Native Code from a VB.NET project

    A couple of things I forget to mention re installing VB6

    1)Right click on Setup.exe and choose Run as administrator with XP SP3 compatibility

    2)When selecting the options you want, don't select Data Access

  10. #10
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Calling a DLL written in VC++.NET Native Code from a VB.NET project

    I installed VB6 without any issue on Win7 64bit (as mentioned you have to run the setup program as an administrator).

  11. #11
    Join Date
    Aug 2014
    Posts
    6

    Re: Calling a DLL written in VC++.NET Native Code from a VB.NET project

    Quote Originally Posted by wavering View Post
    Well, I have tried to install it and failed. On reading around there seems to be a consensus that it cannot be done
    VB6 programming is fine on the Windows 10 technical preview.

    The VB6 IDE installs and runs OK

Tags for this Thread

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