Solved

Calling VB.NET dll from C++ dll

Posted on 2003-11-23
5
3,196 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
  • 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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

IP addresses can be stored in a database in any of several ways.  These ways may vary based on the volume of the data.  I was dealing with quite a large amount of data for user authentication purpose, and needed a way to minimize the storage.   …
This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

809 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