Sign in to follow this  
MagTDK

[.net] Using Unmanaged DLL from managed c++

Recommended Posts

MagTDK    122
I'm just getting into managed c++ and would like to use some existing dll's that I've created in unmanaged c++. I've read some articles that use p/invoke and marshalling to aid in using legacy dll's but am still confused on the implementation. Here is a dummy dll I created to be used in managed c++:
//------------------------------------------------------------------------------
// DummyDLL.h
//------------------------------------------------------------------------------
namespace MyLibrary
{
 class __declspec(dllexport) MyClass
 {
 public:

  std::string Log(std::string name) {

   std::string temp;
   temp = "Hello ";
   temp.append(name);
   return temp;
  }

  int Add(int a, int b) {

   return a+b;
  }
 };
}

int __declspec(dllexport) Add(int a, int b) {

 return a+b;
}

std::string __declspec(dllexport) Log(std::string name) {

 std::string temp;
 temp = "Hello ";
 temp.append(name);
 return temp;
}

//------------------------------------------------------------------------------
// DummyDLL.cpp
//------------------------------------------------------------------------------
#include "stdafx.h"
#include <iostream>
#include "DummyDLL.h"

BOOL APIENTRY DllMain( HANDLE hModule, 
                       DWORD  ul_reason_for_call, 
                       LPVOID lpReserved
      )
{
    return TRUE;
}


And here is an example of using it in unmanaged c++:
#include "stdafx.h"

#include <iostream>
#include "DummyDLL.h"

#pragma comment(lib, "DummyDLL.lib")

using namespace std;
using namespace MyLibrary;

int _tmain(int argc, _TCHAR* argv[])
{
 MyClass library;

 int result = library.Add(5,10);  // calling class from dll
 result = Add(2,4);     // calling function from dll

 string name = library.Log("John"); // calling class from dll using stl strings
 name = Log("Joe");     // calling function from dll using stl strings

 return 0;
}



All of this works fine in unmanaged c++ as expected. What I need is an example of it being used in managed c++. One of the spots I'm having trouble is converting the stl string to a managed string to be used in window forms. Can someone code the above example for managed c++ so I can see what I need to do.

Share this post


Link to post
Share on other sites
nagromo    676
Quote:
Original post by MagTDK
I'm just getting into managed c++ and would like to use some existing dll's that I've created in unmanaged c++. I've read some articles that use p/invoke and marshalling to aid in using legacy dll's but am still confused on the implementation.

Here is a dummy dll I created to be used in managed c++:
*** Source Snippet Removed ***

And here is an example of using it in unmanaged c++:
*** Source Snippet Removed ***

All of this works fine in unmanaged c++ as expected. What I need is an example of it being used in managed c++. One of the spots I'm having trouble is converting the stl string to a managed string to be used in window forms.

Can someone code the above example for managed c++ so I can see what I need to do.


For using STL strings, use the c_str() function to get a char array and use that with a StringBuilder. If MC++ supports attributes the same way C# does (is that part of the language or the framework?) you can just do:


//(Reg C++)
void __declspec(dllexport) MyDllFunc(char *str)
{
...
}

//(MC++)
[DllImport("MyDll.dll")]
static extern void MyDllFunc(StringBuilder str);

and add a reference to MyDll.dll in your MC++ code. You just use the .net equivalent for each unmanaged data type; basic data types are the same, and ref in C# is like *. (I think it's still * in MC++.) If the function uses a char* that is a string, you can just use a StringBuilder on the MC++ side to convert to/from a .net string.

Hope that helped.

[Edited by - nagromo on October 3, 2004 8:07:24 PM]

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this