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.InteropServ
ices
<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.ToStri
ng()
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
Start Free Trial