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
.NET ProgrammingCC++

Avatar of undefined
Last Comment
F. Dominicus

8/22/2022 - Mon
Anurag Agarwal

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

Anurag
ASKER
3mCanada

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

Hi man!

I was having the same problem and here is the solution:
http://support.microsoft.com/kb/828736
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
ASKER CERTIFIED SOLUTION
F. Dominicus

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question