Solved

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

Posted on 2010-09-16
16
1,134 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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
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
 
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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
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…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

822 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