P/Invoke Setup Help

Announcements and discussion for PInvoke

Moderators: Luke Jefferson, Robert

P/Invoke Setup Help

Postby jp2code » Fri Jun 18, 2010 3:36 pm

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: Select all
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: Select all
[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:
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:
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:
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?
Posts: 2
Joined: Fri Jun 18, 2010 3:03 pm
Location: Longview, TX

Postby Paul.Martin » Fri Jun 18, 2010 5:28 pm

The usual cause of the error
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).
Posts: 83
Joined: Wed Feb 03, 2010 11:20 am
Location: Cambridgeshire

Postby jp2code » Sat Jun 19, 2010 7:17 pm

Thanks Mr. Martin!

Fixed that problem, no I'm on to others. :)
Posts: 2
Joined: Fri Jun 18, 2010 3:03 pm
Location: Longview, TX

Return to PInvoke.Net

Who is online

Users browsing this forum: No registered users and 0 guests