Red Gate forums :: View topic - P/Invoke Setup Help
Return to www.red-gate.com RSS Feed Available

Search  | Usergroups |  Profile |  Messages |  Log in  Register 
Go to product documentation
PInvoke.Net
PInvoke.Net forum

P/Invoke Setup Help

Search in PInvoke.Net forum
Post new topic   Reply to topic
Jump to:  
Author Message
jp2code



Joined: 18 Jun 2010
Posts: 2
Location: Longview, TX

PostPosted: Fri Jun 18, 2010 3:36 pm    Post subject: P/Invoke Setup Help Reply with quote

I have a government supplied library written in C++ that is supplied with the original source code, and the files for this code are included as a separate Project in my Visual Studio 2008 Solution.

I followed examples found online on how to call these native routines from my managed Windows application, but the calls fail.

Here is the definition of one of the methods I need to call:
Code:
EXPORT int steadyFor(double Par[], double Inlet[], double Outlet[], char* FileIn, char* FileOut);
From looking at the source code, I have learned the following:

Outlet is an array that I allocate and get results back with.

FileIn and FileOut are both paths to files that contain additional parameters.

I tried creating a wrapper for the library in a "GovProj" class using C# syntax, but it did not work:
Code:
[DllImport(MODELAPP, EntryPoint = "steadyFor", ExactSpelling = false)]
public static extern int SteadyFor(double[] par, double[] inlet, ref double[] outlet, char[] input, char[] output);
The error returned was:
Quote:
Unable to load DLL 'GovProj.dll': The specified module could not be found. (Exception from HRESULT: 0x8007007E)
The "GovProj" is loaded with the rest of my solution, so I guess this means I wrote my Dll Import incorrectly, because the DLL compiles and is included in my solution.

Could someone offer me help with this? I'd love to get on Microsoft's MSDN site, but somehow our old proxy is incompatible with whatever Microsoft is doing. The MSDN sites can't get through our proxy, and work does not care to spend the money for me to have access to code that they feel I should already know how to do. {ugh!}

UPDATE: After more internet research, I found that the GovProj.DLL must be manually copied to my bin/Debug folder. I did that and the "Unable to load DLL" error went away and got replaced with this error:
Quote:
An attempt was made to load a program with an incorrect format. (Exception from HRESULT: 0x8007000B)
I'd like to add the GovProj's output to my project's list of References, but my attempt to do so results in VS2008 telling me:
Quote:
A reference to '{path}\GovProj.dll' could not be added. Please make sure that the file is accessible, and that it is a valid assembly or COM component.
So, how do I make sure my DLL is a valid assembly or COM component?
_________________
~Joe
http://avoidsearshomeimprovement.blogspot.com/
Back to top
View user's profile Send private message
Paul.Martin



Joined: 03 Feb 2010
Posts: 83
Location: Cambridgeshire

PostPosted: Fri Jun 18, 2010 5:28 pm    Post subject: Reply with quote

The usual cause of the error
Quote:

An attempt was made to load a program with an incorrect format

is that you are targeting different platforms with different parts of your solution.


By default C# code is compiled for AnyCPU because it is compiled to an intermediate language and then converted to machine code when it is run. This mean it will run as IA64 code on a IA64 machine and x86 on an x86 machine.
Other the other hand native C++ code is compiled directly to machine code for one particular platform.

The two ways round this is either to compile the native C++ code to each of the different platforms, but this can be a bit of a pain as you have to make sure you distribute the correct version.
The other way is in each of the Project Properties (under the Build tab) for each part of the C# solution change the "Platform target:" to "x86 (or whatever the C++ is being compiled to).
Back to top
View user's profile Send private message
jp2code



Joined: 18 Jun 2010
Posts: 2
Location: Longview, TX

PostPosted: Sat Jun 19, 2010 7:17 pm    Post subject: Reply with quote

Thanks Mr. Martin!

Fixed that problem, no I'm on to others. Smile
_________________
~Joe
http://avoidsearshomeimprovement.blogspot.com/
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic All times are GMT + 1 Hour
Page 1 of 1

 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group