calling managed function from native code.

Hello all,
We all know how to call Native functions from managed code.
Is there other way around. i.e how to call a function in side a
managed code from native (c/c++) code.
This is just for my understanding.

Thanks,
–rc
PS: I know this may be wrong list for such question but this is the
only list I have subscribed.

One way is to make the assembly look like COM server (COM Dll).
Then you can use the it like any COM object. You can google this and you get plenty of resources.
There`s tool(s) you have to run to make the managed DLL look like a COM dll.

http://www.codeproject.com/KB/cs/ManagedCOM.aspx

http://support.microsoft.com/default.aspx?scid=kb;en-us;817248

On 05/30/2011 12:33 AM, Rudra ठाकुर wrote:

[…] how to call a function in side a managed code from native
(c/c++) code. […]

According to Microsoft, you can do this easily:
http://msdn.microsoft.com/en-us/library/ms235292.aspx

"

Easiest is proabably a “mixed assembly” of native and managed code:
http://msdn.microsoft.com/en-us/library/x0w2664k.aspx

You can link to the native code, and call managed code from
within. Most of the marshalling is done automatically for you.

Note that mixed assemblies are not hardware independent, i.e. you need
to provide both 32bit and 64bit versions.

On 05/30/2011 12:33 AM, Rudra ठाकुर wrote:

[…] how to call a function in side a managed code from native
(c/c++) code. […]

According to Microsoft, you can do this easily:
http://msdn.microsoft.com/en-us/library/ms235292.aspx

By default, when compiling with /clr (not /clr:pure),
the definition of a managed function causes the compiler to generate a
managed entry point and a native entry point. This allows the managed
function to be called from native and managed call sites.

Another option is a “mixed assembly” of native and managed code:
http://msdn.microsoft.com/en-us/library/x0w2664k.aspx

Note that mixed assemblies are not hardware independent, i.e. you need
to provide both 32bit and 64bit versions.

[Sorry for the incomplete prior post.]

Thank you guys.

Regards,
–rc

On Mon, May 30, 2011 at 2:41 AM, Hagen Patzke wrote:
> On 05/30/2011 12:33 AM, Rudra ठाकुर wrote:
>> […] how to call a function in side a managed code from native
>> (c/c++) code. […]
>
> According to Microsoft, you can do this easily:
> http://msdn.microsoft.com/en-us/library/ms235292.aspx
>
>> By default, when compiling with /clr (not /clr:pure),
>> the definition of a managed function causes the compiler to generate a
>> managed entry point and a native entry point. This allows the managed
>> function to be called from native and managed call sites.
>
> Another option is a “mixed assembly” of native and managed code:
> http://msdn.microsoft.com/en-us/library/x0w2664k.aspx
>
> Note that mixed assemblies are not hardware independent, i.e. you need
> to provide both 32bit and 64bit versions.

Rudra ठाकुर wrote:

Hello all,
We all know how to call Native functions from managed code.
Is there other way around. i.e how to call a function in side a
managed code from native (c/c++) code.

You need to bear in mind that managed code cannot execute unless the
managed run-time is already present and in control. You can call from a
managed routine to an unmanaged routine without any special
initialization. But to call into managed code, you must already be a
managed process. You can’t just start a normal C++ process and then
call into a managed function. Won’t work.

PS: I know this may be wrong list for such question but this is the
only list I have subscribed.

That’s not a very good excuse. Will you be asking your car repair
questions here next, because this is the only list you have subscribed?


Tim Roberts, xxxxx@probo.com
Providenza & Boekelheide, Inc.

>

That’s not a very good excuse. Will you be asking your car repair
questions here next, because this is the only list you have subscribed?

:slight_smile:

You need to bear in mind that managed code cannot execute unless the
managed run-time is already present and in control. You can call from a
managed routine to an unmanaged routine without any special
initialization. But to call into managed code, you must already be a
managed process. You can’t just start a normal C++ process and then
call into a managed function. Won’t work.

Just for the sake of discussion.

Can’t JIT or something start some managed code runtime on native
code’s behalf .
Then one does not have to go thru COM.

Thanks for you input.

Regards,
–rc

Rudra ठाकुर wrote:

> You need to bear in mind that managed code cannot execute unless the
> managed run-time is already present and in control. You can call from a
> managed routine to an unmanaged routine without any special
> initialization. But to call into managed code, you must already be a
> managed process. You can’t just start a normal C++ process and then
> call into a managed function. Won’t work.
>
Just for the sake of discussion.

Can’t JIT or something start some managed code runtime on native
code’s behalf .
Then one does not have to go thru COM.

Sort of. You can call CorBindToRuntimeEx (there’s a new interface for
.NET 4) to load the CLR into an unmanaged process. There are a couple
of COM interfaces you must implement to act as a “host” for the CLR.
Even then, however, you cannot just “call” the function, because you
can’t link to it. You have to ask the runtime to load the assembly and
run the function for you.


Tim Roberts, xxxxx@probo.com
Providenza & Boekelheide, Inc.

Thanks Tim,
I understand.

we all know that how simple it is for win32 app to call function in
side native dll and how simple it is for manged app to call function
inside native dll.
It should have been that simple to call function inside manged dll
from native app.

Here is the link I found which is kind of more useful.
http://www.codeproject.com/KB/COM/simpleclrhost.aspx

Thanks for the pointer,
–rc

On Tue, May 31, 2011 at 4:42 PM, Tim Roberts wrote:
> Rudra ठाकुर wrote:
>>
>>> You need to bear in mind that managed code cannot execute unless the
>>> managed run-time is already present and in control. You can call from a
>>> managed routine to an unmanaged routine without any special
>>> initialization. But to call into managed code, you must already be a
>>> managed process. You can’t just start a normal C++ process and then
>>> call into a managed function. Won’t work.
>>>
>> Just for the sake of discussion.
>>
>> Can’t JIT or something start some managed code runtime on native
>> code’s behalf .
>> Then one does not have to go thru COM.
>
> Sort of. You can call CorBindToRuntimeEx (there’s a new interface for
> .NET 4) to load the CLR into an unmanaged process. There are a couple
> of COM interfaces you must implement to act as a “host” for the CLR.
> Even then, however, you cannot just “call” the function, because you
> can’t link to it. You have to ask the runtime to load the assembly and
> run the function for you.
>
> –
> Tim Roberts, xxxxx@probo.com
> Providenza & Boekelheide, Inc.
>
>
> —
> NTDEV is sponsored by OSR
>
> For our schedule of WDF, WDM, debugging and other seminars visit:
> http://www.osr.com/seminars
>
> To unsubscribe, visit the List Server section of OSR Online at http://www.osronline.com/page.cfm?name=ListServer
>

Can we PLEASE not have this discussion here?

I’m mean… I’m even interested in the topic (sort of)… but entirely off-topic.

The OP knows this… the follow-up posters know this.

Peter
OSR

P.S. Thread locked.