PC Review


Reply
Thread Tools Rate Thread

Calling managed code in a class library from a standard DLL

 
 
Bruno Neves Pires Silva
Guest
Posts: n/a
 
      11th Dec 2008
Hello, programmers.

How can I use the code in a managed dll from a standard Dll,
without using COM ?

Thanxs in advance

Bruno Neves Pires Silva
 
Reply With Quote
 
 
 
 
Jeroen Mostert
Guest
Posts: n/a
 
      11th Dec 2008
Bruno Neves Pires Silva wrote:
> How can I use the code in a managed dll from a standard Dll,
> without using COM ?
>

Use C++/CLI and export plain old unmanaged functions that wrap around your
managed code.

--
J.
 
Reply With Quote
 
 
 
 
Bruno Neves Pires Silva
Guest
Posts: n/a
 
      12th Dec 2008
On 11 dez, 17:01, Jeroen Mostert <jmost...@xs4all.nl> wrote:
> Bruno Neves Pires Silva wrote:> * *How can I use the code in a managed dll from a standard Dll,
> > without using COM ?

>
> Use C++/CLI and export plain old unmanaged functions that wrap around your
> managed code.
>
> --
> J.


Have you tried to do this before, Jeroen?

Can you post a example? I have tried the following code, but it didnt
work:

#pragma once

using namespace System;
using namespace System::Windows::Forms;

namespace MixedDllTest1 {

public ref class Class1
{
public:

static void Test()
{
System::Windows::Forms::MessageBox::Show("This is a
test");
}
};
}
extern "C" void __declspec(dllexport) DllTest()
{
MixedDllTest1::Class1::Test();
}

#define far
typedef void far *LPVOID;

int __stdcall dllMain( void *hModule,
unsigned long ul_reason_for_call,
LPVOID lpReserved
)
{
return 1;
}


#define DllEntryPoint
 
Reply With Quote
 
Jeroen Mostert
Guest
Posts: n/a
 
      12th Dec 2008
Bruno Neves Pires Silva wrote:
> On 11 dez, 17:01, Jeroen Mostert <jmost...@xs4all.nl> wrote:
>> Bruno Neves Pires Silva wrote:> How can I use the code in a managed dll from a standard Dll,
>>> without using COM ?

>> Use C++/CLI and export plain old unmanaged functions that wrap around your
>> managed code.
>>

>
> Have you tried to do this before, Jeroen?
>

No, never. I have boundless confidence in this working, though. :-)

It turns out my advice is misleading; you don't export unmanaged functions
since you can't call managed code from unmanaged functions (duh). Exporting
the managed functions directly works.

> Can you post a example?


Yes. I'm going to go command-line, if you don't mind, as it's more
reproducible than project files. You may have to puzzle around a bit if you
want to use the GUI to achieve the same results.

We'll have three artifacts: a managed DLL, an unmanaged DLL linking to the
managed DLL and a little EXE to call a function in the unmanaged DLL.

ManagedLibrary.cpp:

using namespace System;

ref class Foo {
public:
static void Test() {
Console::WriteLine("Hello from the managed world.");
}
};

extern "C" __declspec(dllexport) void __stdcall test(void) {
Foo::Test();
}

UnmanagedLibrary.cpp:

#include <stdio.h>
extern "C" __declspec(dllimport) void __stdcall ManagedHello(void);

extern "C" __declspec(dllexport) void __stdcall hello(void) {
printf("Hello from the unmanaged world.\n");
ManagedHello();
}

program.cpp:

#include <stdio.h>
extern "C" __declspec(dllimport) void __stdcall hello(void);

int main() {
hello();
}


Now let's link it all together. First the managed DLL:

cl /clr ManagedLibrary.cpp /link /dll /out:ManagedLibrary.dll

We're not done! We need to embed the library manifest in the DLL, because
for some reason the linker won't do it for us:

mt /manifest ManagedLibrary.dll.manifest
/outputresource:ManagedLibrary.dll;#2

Without this step, you will get CRT initialization errors. Now the unmanaged
DLL:

cl UnmanagedLibrary.cpp ManagedLibrary.lib /link /dll
/out:UnmanagedLibrary.dll

Finally the EXE:

cl program.cpp UnmanagedLibrary.lib

Running the EXE:

Hello from the unmanaged world.
Hello from the managed world.

And that's all there is to it! Well, when it works. :-)

--
J.
 
Reply With Quote
 
Ben Voigt [C++ MVP]
Guest
Posts: n/a
 
      16th Dec 2008


"Jeroen Mostert" <(E-Mail Removed)> wrote in message
news:4942c90a$0$194$(E-Mail Removed)...
> Bruno Neves Pires Silva wrote:
>> On 11 dez, 17:01, Jeroen Mostert <jmost...@xs4all.nl> wrote:
>>> Bruno Neves Pires Silva wrote:> How can I use the code in a managed
>>> dll from a standard Dll,
>>>> without using COM ?
>>> Use C++/CLI and export plain old unmanaged functions that wrap around
>>> your
>>> managed code.
>>>

>>
>> Have you tried to do this before, Jeroen?
>>

> No, never. I have boundless confidence in this working, though. :-)
>
> It turns out my advice is misleading; you don't export unmanaged functions
> since you can't call managed code from unmanaged functions (duh).
> Exporting


Well, you can as long as they haven't got any managed types in the
signature, using a variety of mechanisms, such as function prototypes, dll
imports, function pointers, etc.

> the managed functions directly works.



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Giving one managed wrapper class access to the unmanged part of another managed wrapper class mschuck Microsoft VC .NET 1 31st Aug 2006 05:27 PM
Reference and tutorials using/calling native code (dll) from managed code app Mario Microsoft Dot NET Compact Framework 2 2nd Aug 2006 05:06 PM
Catching standard library exceptions in managed code (2nd post) Epetruk Microsoft VC .NET 0 3rd Oct 2004 02:10 AM
Catching standard library exceptions in managed code Wild Wind Microsoft VC .NET 0 29th Sep 2004 12:18 AM
Calling DLL export function(unmanaged visual c++ code) from managed code(Visual c#,.net) =?Utf-8?B?c2Vhc2g=?= Microsoft C# .NET 1 4th Feb 2004 11:16 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 05:54 AM.