Solved

Calling VB.NET dll from C++ dll

Posted on 2003-11-23
5
3,205 Views
Last Modified: 2010-08-05
Im having a very hard time finding anything that can either how to do this or if it is even possible - I want to call a VB.NET dll from within a plain old C++ dll (not .NET!).

I have a sneaking suspicion it simply cant be done.

Ive read a bit about managed and unmanaged code and I think this is where the problem is.
0
Comment
Question by:MrNed
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 12

Expert Comment

by:vascov
ID: 9807676
One option is to use COM Interop.

Export your types into a typelib. Then use #import to import the types. No need for Managed extensions.

HTH
0
 
LVL 12

Accepted Solution

by:
vascov earned 150 total points
ID: 9807760
Here's a sample:

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

HTH
0
 
LVL 7

Author Comment

by:MrNed
ID: 9807952
Thanks!!!!

I actually understand very little of that, but I'll have a play and see what I can come up with.
0
 
LVL 12

Expert Comment

by:vascov
ID: 9813017
Let me know of your progress...

If you have a specific situation you want to address, you're welcomed.

Thx
0
 
LVL 7

Author Comment

by:MrNed
ID: 9823328
Thanks for your help. We got it working based on what you provided.
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

688 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question