Mixing C# and native code in the same DLL?

N

Norbert Unterberg

I have successfully created a native PocketPC C++ DLL and a C# wrapper class to
use the DLL from managed .NET code. It all works fine now.

Now I wonder if there is any way to pack the two components together into one
DLL file that can be used like a standard .NET assembly and can be referenced by
any .NET project. It would simplify deployment if there were no need to take
care of an additional native DLL file.

Is it possible to create a single "mixed mode" DLL that acts like a C# assembly
and also contains the native code that is called via Platform Invoke using
[DllImport("nativelib.dll")]?

Thank you
Norbert
 
C

Chris Tacke, eMVP

Not directly possible, no. A strategy you can use, which I've done
successfully before, is to put the native DLL(s) into the managed assembly
as an embedded resource, then extract it to disk at run-time if necessary.
It decreases number of files deployed, but it increases footprint since you
effectively have 2 copies of the native DLL one as a resource and one that's
actually used.


--

Chris Tacke, Embedded MVP
OpenNETCF Consulting
Giving back to the embedded community
http://community.OpenNETCF.com
 
A

Arun

Here is an article from Daniel Moth on the same topic,
Check this out.
http://www.danielmoth.com/Blog/2004/09/share-code-if-fullframe_17.html

Better use word "Sharing code" and do a google compact framework
group search
http://groups.google.com/advanced_group_search
you will get lots of threads discussed the same topic and few will
really help you to start with.

Hope this helps,
Cheers,
Arun.



Not directly possible, no.  A strategy you can use, which I've done
successfully before, is to put the native DLL(s) into the managed assembly
as an embedded resource, then extract it to disk at run-time if necessary.
It decreases number of files deployed, but it increases footprint since you
effectively have 2 copies of the native DLL one as a resource and one that's
actually used.

--

Chris Tacke, Embedded MVP
OpenNETCF Consulting
Giving back to the embedded communityhttp://community.OpenNETCF.com




I have successfully created a native PocketPC C++ DLL and a C# wrapper
class to use the DLL from managed .NET code. It all works fine now.
Now I wonder if there is any way to pack the two components together into
one DLL file that can be used like a standard .NET assembly and can be
referenced by any .NET project. It would simplify deployment if there were
no need to take care of an additional native DLL file.
Is it possible to create a single "mixed mode" DLL that acts like a C#
assembly and also contains the native code that is called via Platform
Invoke using [DllImport("nativelib.dll")]?
Thank you
Norbert- Hide quoted text -

- Show quoted text -
 
C

Chris Tacke, eMVP

He's not asking how to share code assets. He's asking how to combine
managed and native code. It's a completely different concept that requires
a completely different solution.


--

Chris Tacke, Embedded MVP
OpenNETCF Consulting
Giving back to the embedded community
http://community.OpenNETCF.com

Here is an article from Daniel Moth on the same topic,
Check this out.
http://www.danielmoth.com/Blog/2004/09/share-code-if-fullframe_17.html

Better use word "Sharing code" and do a google compact framework
group search
http://groups.google.com/advanced_group_search
you will get lots of threads discussed the same topic and few will
really help you to start with.

Hope this helps,
Cheers,
Arun.



Not directly possible, no. A strategy you can use, which I've done
successfully before, is to put the native DLL(s) into the managed assembly
as an embedded resource, then extract it to disk at run-time if necessary.
It decreases number of files deployed, but it increases footprint since
you
effectively have 2 copies of the native DLL one as a resource and one
that's
actually used.

--

Chris Tacke, Embedded MVP
OpenNETCF Consulting
Giving back to the embedded communityhttp://community.OpenNETCF.com




I have successfully created a native PocketPC C++ DLL and a C# wrapper
class to use the DLL from managed .NET code. It all works fine now.
Now I wonder if there is any way to pack the two components together
into
one DLL file that can be used like a standard .NET assembly and can be
referenced by any .NET project. It would simplify deployment if there
were
no need to take care of an additional native DLL file.
Is it possible to create a single "mixed mode" DLL that acts like a C#
assembly and also contains the native code that is called via Platform
Invoke using [DllImport("nativelib.dll")]?
Thank you
Norbert- Hide quoted text -

- Show quoted text -
 
C

Christopher Fairbairn

Hi,

The System.Data.SQLite ADO.NET provider is one example of an assembly that
acheives this goal while targeting the Compact Framework.

However it seems to be more trouble than it's worth. See for example the
recent thread on the SQLite forums
http://sqlite.phxsoftware.com/forums/t/1187.aspx where they are considering
dropping the feature.

Microsoft's ADO.NET providers for SQL Server or SQL Server Compact have a
similiar need of matching a managed wrapper with a native layer. In their
case they package the individual files into a CAB file and then register
with Visual Studio so a reference to the managed assembly automatically
downloads and installs the CAB file while deploying via the IDE. So the IDE
experience is roughly similiar.

Hope this helps,
Christopher Fairbairn
 
A

Arun

Yes, my mistake..didn't peruse the question properly the first time.
Thanks for clearing up that Chris.


He's not asking how to share code assets.  He's asking how to combine
managed and native code.  It's a completely different concept that requires
a completely different solution.

--

Chris Tacke, Embedded MVP
OpenNETCF Consulting
Giving back to the embedded communityhttp://community.OpenNETCF.com


Here is an article from Daniel Moth on the same topic,
Check this out.http://www.danielmoth.com/Blog/2004/09/share-code-if-fullframe_17.html

Better use word "Sharing code" and do a google compact framework
group searchhttp://groups.google.com/advanced_group_search
you will get lots of threads discussed the same topic and few will
really help you to start with.

Hope this helps,
Cheers,
Arun.

Not directly possible, no. A strategy you can use, which I've done
successfully before, is to put the native DLL(s) into the managed assembly
as an embedded resource, then extract it to disk at run-time if necessary.
It decreases number of files deployed, but it increases footprint since
you
effectively have 2 copies of the native DLL one as a resource and one
that's
actually used.

Chris Tacke, Embedded MVP
OpenNETCF Consulting
Giving back to the embedded communityhttp://community.OpenNETCF.com
I have successfully created a native PocketPC C++ DLL and a C# wrapper
class to use the DLL from managed .NET code. It all works fine now.
Now I wonder if there is any way to pack the two components together
into
one DLL file that can be used like a standard .NET assembly and can be
referenced by any .NET project. It would simplify deployment if there
were
no need to take care of an additional native DLL file.
Is it possible to create a single "mixed mode" DLL that acts like a C#
assembly and also contains the native code that is called via Platform
Invoke using [DllImport("nativelib.dll")]?
Thank you
Norbert- Hide quoted text -
- Show quoted text -- Hide quoted text -

- Show quoted text -
 
R

Robert Simpson

Yep it can be done, but it was a pain in the butt.

Basically I create a "hole" in the native DLL, read the pure managed
assembly in, write it into the hole, add a COR20 header to the native DLL,
fixup all the .net metadata and code, and rewrite the entrypoints.

It works as long as you don't need a signed assembly. The Compact Framework
has a bug in the digital signature verification. Certain signed mixed
assemblies like I've made above will pass verification on the desktop, but
fail on the CF.

https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=319532

Included in the System.Data.SQLite source code distribution is the
"mergebin" utility I wrote that will (depending on the command-line
parameters) either write out a .h file for you to include in your native
source code (which makes the hole), or merge a native DLL and a pure managed
DLL.

Robert
 
N

Norbert Unterberg

Robert said:
Yep it can be done, but it was a pain in the butt.
[...]

Thank you all for your helpful replies. At least I know where I stand ... and
that it is more than finding the right switch in the IDE ...

Norbert
 
J

jp2msft

I've never tried creating a mixed-mode CAB file, but Visual Studio does
include a setup for Smart Phones.

In your project, right-click and add new project. It is either under Smart
Devices or under Other. I know it is in Other, but the Other folder may be
listed under the Smart Devices. Follow the Wizard and before you know it
you'll have a CAB file.

Hope that helps!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top