Solved

How can I run a 32 bit unmanaged dll from 64 bit C# app

Posted on 2010-09-16
16
1,075 Views
Last Modified: 2012-05-10
I am not a C# programmer (I am more C++, Windows programming)...so my question has to do with the interface a 64 bit .net object uses to access a 32 bit unmanaged dll.  

I know that in order to access a 32 bit unmanaged dll from a 64 bit unmanaged application, an RPC mechanism, such as DCOM, can be used, where the data is transferred to a surrogate process by marshalling.

With regard to a managed application...can a .Net app use the same DCOM interface used by the unmanaged application, or is there some other mechanism that must be used?
0
Comment
Question by:monstro281
  • 7
  • 3
  • 2
  • +2
16 Comments
 
LVL 4

Expert Comment

by:timexist
ID: 33697686
it will very a compilcate issue.
I faced a similar issue a while ago. a 32bit dll to open the cashdraw cannot work on x64 windows 7 machine.
It is too complicate to deal with it. There is no easy answer for this.
At last I found a simple solution .
I changed my project to target x86 cpu, it means everything go back to 32bit again, and x64 windows 7 is happy too.
So what I talking about here is that you can change your project to 32 bit to fit the out-of-date old 32bit dll.

0
 

Author Comment

by:monstro281
ID: 33697745
Yes...I am aware of this, but it is not an option for me.  On 64 bit platforms, the app will be 64 bit, but the dll's will remain 32 bit.  

Can the platform invoke method be used for RPC, or can .Net objects implement DCOM?
0
 
LVL 4

Expert Comment

by:timexist
ID: 33697776
On 64-bit platforms, application can be 32bit, for windows just as default, it regard everything as 64 bit.
do you know CorFlags.exe? it can put a flag on the application, mark it as 32bit or not.
I solved another 64/32 issue, by using this tool only.
If you have to go com/dcom way, I have to say good luck and have fun.


0
 
LVL 12

Expert Comment

by:trinitrotoluene
ID: 33699068
take a look at the code sample here.

http://software.intel.com/en-us/articles/access-a-32-bit-dll-from-a-native-64-bit-process-on-intel-architecture/

I'll get back with more stuff later today
0
 

Author Comment

by:monstro281
ID: 33699097
Too bad the download link is broken....it looked promising.
0
 
LVL 12

Expert Comment

by:trinitrotoluene
ID: 33700644
oh thats bad, I thought it didn't go thru since my conn was bad. i'll try to give u the same info.....i thought a link would be easier...
0
 
LVL 24

Expert Comment

by:alexey_gusev
ID: 33742913
>> On 64 bit platforms, the app will be 64 bit, but the dll's will remain 32 bit.  

why is that? just change the target to x86 as it was suggested already and all will be fine. It worked perfectly well with managed/unmanaged code.
0
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
LVL 13

Expert Comment

by:Corey2
ID: 33744556
I have used .Net remoting in the past to accomplish this.  You need a second process to instanciate an instance of the class then you remote to it.  I suggest creating a wrapper class that the x64 code will communicate with.

The gist of it is you create a class on one side of the fence then using remoting you get a pointer to that class then you make calls to it from the x64 process just like it was created there.

http://www.experts-exchange.com/Programming/Languages/.NET/Visual_Basic.NET/Q_22071190.html?sfQueryTermInfo=1+net+remot
0
 

Author Comment

by:monstro281
ID: 33746628
alexey...as I said before, had you read my response, building in 32 bit is NOT and option.  It is a 3rd party application...and their guidelines restrict the app to be built with the "Any CPU" option.  I have to make it work with 32 bit dll's.  That is the bottom line.

0
 

Author Comment

by:monstro281
ID: 33746780
Corey2...does this solution work between managed and unmanaged code?
0
 
LVL 13

Accepted Solution

by:
Corey2 earned 500 total points
ID: 33747257
You will have to create a x86 .Net exe that will create the 32bit object and expose it to x64.  The 32bit object can be managed or un-managed doesn't really matter.  Worst case you create a managed wrapper for the methods you need.  I would personally create a managed wrapper, matter of preference.
0
 

Author Comment

by:monstro281
ID: 33749924
Corey2...thanks for the input.  After researching this, it is possible to solve this problem using C# remoting.  The only problem with this approach is the complexity of exposing the x86 .Net exe (which hosts the 32 bit dll) to an unmanaged client.  It should be possible to communicate to the .Net host via COM...but I consider that more complex than simply using something like DCOM in an unmanaged client.

At this time, I am going to explore hosting the 32 bit dll in an unmanaged COM server.  I will expose the dll interface in a COM style wrapper implemented in the 64 bit .Net app, using DCOM to communicate with the COM server.  This will provide the easiest to implement, most general solution (in my humble opinion).  
0
 

Author Closing Comment

by:monstro281
ID: 33749942
Corey2's response was a viable solution...but I am searching for an even more general solution.  Corey2's response lead me to the solution I was looking for...so he gets the A.
0
 
LVL 4

Expert Comment

by:timexist
ID: 33750042
I am glad you can find something you are looking for.
Could please you explain what it is?
0
 

Author Comment

by:monstro281
ID: 33756664
timexist...the solution involves a remote procedure call type mechanism.  This can be accomplished by a few methods.  Corey2 suggested C# Remoting.  This is viable for the purpose I need, but is not the most general solution I could find.  It led me to conclude that the most general solution for managed/unmanaged code would be to use COM in an out of process server...distributed COM.  

Here is a link to a similar solution:

http://social.msdn.microsoft.com/Forums/en-US/vcgeneral/thread/f2c10d58-ec35-4e7c-99dd-3d6c71c30112

Mike Becker also posted his own take a few years back:

http://www.dnjonline.com/article.aspx?id=jun07_access3264

The beauty of this solution is it can easily be used in both managed and unmanaged code.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now