CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 13 of 13
  1. #1
    Join Date
    Apr 2002
    Location
    United Kingdom
    Posts
    310

    using .NET DLL with VS6

    I have written a managed dll written in c++ .NET, I now need to use this dll from vc++ which is unmanaged, what is the procedure for this and draw backs if any?

    Thanks in advance

  2. #2
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,620

    Re: using .NET DLL with VS6

    .NET module could be used in unmanaged code only via proxy library which gives to module ability to mimic COM-module behavior. Such proxy dll could be built with the regasm.exe utility (or tlbexp.exe with some extra body motions).

    I'm not an expert in the question but I'm hear the talkings next to me already a couple of days... And I've got a couple of books on .NET and took a look at them at bedtime.
    Best regards,
    Igor

  3. #3
    Join Date
    Apr 2002
    Location
    United Kingdom
    Posts
    310

    Re: using .NET DLL with VS6

    Thanks for the reply, still looking for an answer, the only way I can see it to be done is to write a bridge dll, which is the last thing I want to do, I just want an easy way of using my managed C++ dll with unmanaged C++.

    The problem I have is that I have a lot of legacy code that cannot be-written very easily, the code relies heavely on MFC, and I have written an managed C++ dll that is using the great .NET model, my new projects are being written in .NET.

    Thanks again.....

  4. #4
    Join Date
    Apr 2002
    Location
    United Kingdom
    Posts
    310

    Re: using .NET DLL with VS6

    Worked out a work around, I have exported a bridge function from the managed dll, now my managed dll should be able to be used by any language.

    The following code exports a single function 'bridge_function', this function then creates a reference to an object then sends an email by SMTP, it works like a dream.

    Code:
    Managed DLL code (MyExports.cpp)...
    
    #include "stdafx.h"
    #include "SRGtool_lib.h"
    
    #pragma once
    
    using namespace System;
    using namespace SRGtool_lib;
    
    extern "C"
    {
      __declspec(dllexport) void __cdecl bridge_Email_SMTP( char * pszFrom, char * pszTo, char * pszSubject, char * pszBody ) 
     {
    	SRGtools __gc * tools = new SRGtools();
    
    	String __gc *sFrom		= new String( pszFrom );
    	String __gc *sTo		= new String( pszTo );
    	String __gc *sSubject	= new String( pszSubject );
    	String __gc *sBody		= new String( pszBody );
    
    	tools->Email_SMTP( sFrom, sTo, sSubject, sBody, NULL, TRUE );
     }
    }
    Code:
    My unmanaged client app...
    
    #include "stdafx.h"
    
    #include <windows.h>
    
    typedef void (CALLBACK* LPFNDLLFUNC)(void);
    
    int _tmain(int argc, _TCHAR* argv[])
    {
    	HINSTANCE hDLL;
    	LPFNDLLFUNC lpfnDllFunc;
    
    	hDLL = LoadLibrary( "..\\common\\SRGtool_lib.dll" );
    	if( !hDLL )
    	{
    		printf( "Cannot load dll\n" );
    		return 1;
    	}
    
    	lpfnDllFunc = (LPFNDLLFUNC)GetProcAddress( hDLL, "bridge_Email_SMTP" );
    	if( !lpfnDllFunc )
    	{
    		printf( "Failed to get proc address\n" );
    		FreeLibrary( hDLL );
    		return 1;
    	}
    
    	// call function
    	lpfnDllFunc( "Me", "shaun@somewhere.com", "subject", "body" );
    
    	FreeLibrary( hDLL );
    
    	return 0;
    }
    The work now starts in converting data types from unmanaged to managed and visa versa, so if anyone can assist in the procedure I would be grateful,
    I would first like to convert a CString to a String and also pass arrays of strings, ints etc...

    Thanks in advance....

  5. #5
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,620

    Smile Re: using .NET DLL with VS6

    Quote Originally Posted by Filbert Fox
    Worked out a work around, I have exported a bridge function from the managed dll, now my managed dll should be able to be used by any language.

    ...

    The work now starts in converting data types from unmanaged to managed and visa versa, so if anyone can assist in the procedure I would be grateful,
    I would first like to convert a CString to a String and also pass arrays of strings, ints etc...
    I respect your stubbornness deeply. And it's the last time I repeat my advice to generate proxy COM-dll. The side effect of this action is very useful - there type library will be generated, and you will be able at unmanaged side to use COMpatible types which will be converted by proxy to .NET types transparently.
    Otherwise you definitely need to do all type transformations "hand-craftily".

    PS. Another nice effect is the ability to use all available assembly classes as COM-objects (with some restrictions). You never need to "bridge" and "bond" - you just come and take.
    PPS. Of course things not so easy - there always some threshold exists you have to step over. And the question is - does the effort cost a result.
    Best regards,
    Igor

  6. #6
    Join Date
    Apr 2002
    Location
    United Kingdom
    Posts
    310

    Re: using .NET DLL with VS6

    I am not stubborn, just new to .NET and having to deal with the problem I have, I have taken your suggestion on board and will be looking at it very shortly, the creating of the proxy COM-dll might be the answer but I needed to get some code working and exporting a bridge function solved the problem, remember there is more than one road to a destination.

  7. #7
    Join Date
    Apr 2002
    Location
    United Kingdom
    Posts
    310

    Re: using .NET DLL with VS6

    Hi Guys, Mr Stubborn here!!!!

    Created a type library from my .NET DLL, using regasm, when I came to import the type library into a test unmanaged project, I have got a few problems, the TLH file was created with no problem, but trying to create an instance seemed to cause a problem the error was saying No Interface.

    Code:
    #include "stdafx.h"
    
    #import "..\SRGtool_lib\Debug\SRGtool_lib.tlb" raw_interfaces_only
    
    int _tmain(int argc, _TCHAR* argv[])
    {
    	::CoInitialize( NULL );
    
    	SRGtool_lib::_SRGaddrTool * pTool;
    
    	SRGtool_lib::_SRGaddrToolPtr p( __uuidof(SRGtool_lib::_SRGaddrTool) ); <<<<< ERROR HERE!!!!!
    
    	pTool = p;
    
    	// start using tool
    
    	::CoUninitialize();
    
    	return 0;
    }
    Any Ideas???

  8. #8
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,620

    Re: using .NET DLL with VS6

    Well, I have done a little sample - though I was very short of time I was extremely interested, because I heard a lot about that "inability to create instance" and "no interface registered" or something like that...
    I don't know why my sample was so obedient - it was started at once. Is it too simple?
    Attached Files Attached Files
    Best regards,
    Igor

  9. #9
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,620

    Re: using .NET DLL with VS6

    By the way, Filbert, thank you for the topic - I had intension to touch .NET, and that intension was lasted very long time, but I had really touched it only this afternoon.

    PS. I found I was wrong - there are no additional dlls produced during regasm'ing .NET module - it's just proper registration record generated. Sorry, I'm only .NET-beginner.
    Last edited by Igor Vartanov; January 6th, 2005 at 02:55 AM.
    Best regards,
    Igor

  10. #10
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,620

    Re: using .NET DLL with VS6

    Second variant of C++ client (for my previous sample)

    Code:
    #include <comip.h>
    #import "C:\WINDOWS\Microsoft.NET\Framework\v1.1.4322\System.Windows.Forms.tlb" no_namespace
    #import "C:\WINDOWS\Microsoft.NET\Framework\v1.1.4322\mscorlib.tlb"
    #import "dncl.tlb" no_namespace 
    #pragma comment(lib, "comsupp")          
    
    void main()
    {
    	::CoInitialize(NULL);
    
    	_CSharpSimplePtr simp( __uuidof(CSharpSimple) );
    	simp->Show( _bstr_t("Test String") );
    	ISimplePtr isimp( __uuidof(CSharpSimple) );
    	DialogResult res = isimp->Show( _bstr_t("Show Result?"), _bstr_t("Another Test"), MB_YESNO );
    
    	if( IDYES == res)
    		simp->Show( _bstr_t("YES was pressed") );
    	::CoUninitialize();
    }
    Best regards,
    Igor

  11. #11
    Join Date
    Apr 2002
    Location
    United Kingdom
    Posts
    310

    Re: using .NET DLL with VS6

    Igor, There seems to be a problem when using C++ .NET, I created a test project using C# and it worked first time? Have you tried to write a managed DLL using C++, I am now banging my head on the desk in dismay!!! I thought .NET was a framework that can be used by C++, C#, VB etc...

  12. #12
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,620

    Re: using .NET DLL with VS6

    Quote Originally Posted by Filbert Fox
    Have you tried to write a managed DLL using C++,
    Never.
    As I already mentioned I'm very busy just right now but when I get some break I'll try to implement my sample in managed C++.
    I thought .NET was a framework that can be used by C++, C#, VB etc...
    Well, I thought that way too...
    Best regards,
    Igor

  13. #13
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,620

    Re: using .NET DLL with VS6

    As I promised, dotNet module implemented in managed C++ and used from C# and unmanaged C++.

    Does it help?
    Attached Files Attached Files
    Best regards,
    Igor

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured