Link to home
Start Free TrialLog in
Avatar of 3mCanada
3mCanadaFlag for Canada

asked on

How can I call a VB.net method from within a C++ Win32 application?

I have a single function written in VB.net that I need to call from a C++ program.  (It is a complex function that processes an excel spreadsheet; recoding it into c++ would be non-trivial.)  What would be the best way to achieve this?  I have to pass multiple parameters both ways.

I tried executing the following sample program listed in question ID: 9807676, but could not get it to work.  
The VB code built OK, and the sn..., regasm... and gacutil... command line calls claimed the succeeded.  

The C code would not compile as posted, listing 'AClass' in the following line as an undeclared identifier.
           pIAClass.CoCreateInstance( __uuidof( AClass ) );   // Create the object
Changing 'AClass' to 'IAClass' allowed it to compile OK, (though that was just a guess at how to fix it.)
However, the pIAClass.CoCreateInstance... call fails at runtime with error code REGDB_E_CLASSNOTREG.  


VB.NET Lib:
Imports System
Imports System.Reflection
Imports System.Runtime.InteropServices

<Assembly: AssemblyKeyFile( "mykey.snk" )>

< ComVisible( true ) > _
Public Interface IAClass
      Function GetCurrentTime() As String
End Interface

< ProgId( "MyCompany.MyLib.AClass" ) > _
Public Class AClass
      Implements IAClass

      Public Function GetCurrentTime() As String Implements IAClass.GetCurrentTime
            Return System.DateTime.Now.ToString()
      End Function
End Class

To compile the MyLib.vb file:
sn -k mykey.snk
vbc /t:library MyLib.vb

Then to "deploy" (register it with COM to make it available for import in C++)
regasm /tlb:MyLib.tlb MyLib.dll
gacutil -i MyLib.dll

(1st we register it in the COM registry, and ask for a .tlb file to use with our Cpp code)
(2nd we make the Assembly global so that any instantiation can find it)


Now onto our Cpp code:
#include <windows.h>
#include <atlbase.h>
#include <stdio.h>

#import "./Mylib.tlb"  // import the tlb we generated previously

int main()
{
      /* DEMO CODE - NO ERROR HANDLING */
      CoInitialize( 0 );   // Initialize COM
      {
            using namespace MyLib;    // save us from the trouble of always doing MyLib::IAClass, etc...

            CComPtr< IAClass > pIAClass;
            pIAClass.CoCreateInstance( __uuidof( AClass ) );   // Create the object

            _bstr_t bstrResult = pIAClass->GetCurrentTime();  // Call our .NET func and get the result

            printf( "Current DateTime: %s\n", (char*)bstrResult );
      }
      CoUninitialize();
}

compiling this is trivial:
cl MyApp.cpp
Avatar of Anurag Agarwal
Anurag Agarwal
Flag of India image

Just a thought.. you can make a dll / COM dll using this VB.Net function & then call it from C++

Anurag
Avatar of 3mCanada

ASKER

Dear Anurag,
I'm a bit unclear as to what you are suggesting I do differently.  (I thought that that was the intent of the example code posted with the question.).
Avatar of anmalaver
anmalaver

Hi man!

I was having the same problem and here is the solution:
http://support.microsoft.com/kb/828736
ASKER CERTIFIED SOLUTION
Avatar of F. Dominicus
F. Dominicus
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial