From: jp2msft on
I've got a library from a government agency that only comes with the .lib and
a header (.h) file.

The header shows me what methods are exposed, but I'm not sure if I can get
to those features through a .Net Framework.

If I want to call one of the four (4) exported methods of the DLL, how would
I go about formatting the P/Invoke call to them?

Here is an example of one of the exposed methods (from the header):

extern void standardCalc(double param(), double inputs(), double outputs());

I'm getting rather used to C# syntax, but I think "double param()" is an
array of doubles - what I'd call "double[] param" in C#. If I am mistaken,
please correct me!

So, how would I go about invoking this exposed method from the library file
"aesra10.lib"?

Also, all examples of P/Invoke only list the file name, not it's location.
Do all files that I invoke this way have to be in a certain folder or can I
direct it to the folder where my application will be storing this DLL?
From: Jason Keats on
jp2msft wrote:
> I've got a library from a government agency that only comes with the .lib and
> a header (.h) file.
>
> The header shows me what methods are exposed, but I'm not sure if I can get
> to those features through a .Net Framework.
>
> If I want to call one of the four (4) exported methods of the DLL, how would
> I go about formatting the P/Invoke call to them?
>
> Here is an example of one of the exposed methods (from the header):
>
> extern void standardCalc(double param(), double inputs(), double outputs());
>
> I'm getting rather used to C# syntax, but I think "double param()" is an
> array of doubles - what I'd call "double[] param" in C#. If I am mistaken,
> please correct me!
>
> So, how would I go about invoking this exposed method from the library file
> "aesra10.lib"?
>
> Also, all examples of P/Invoke only list the file name, not it's location.
> Do all files that I invoke this way have to be in a certain folder or can I
> direct it to the folder where my application will be storing this DLL?

I'm pretty sure you can't directly access functions within the static
library file aesra10.lib.

You will first need to use C/C++ to link it into a dynamic (.dll)
library of your making. You should then be able to use P/Invoke from C#
to access its functions.

Once you've made a dll you should put it in the same folder as your program.
From: Jeff Johnson on
"jp2msft" <jp2msft(a)discussions.microsoft.com> wrote in message
news:666994F8-19A3-4B6B-8E43-4AA4C8D8235E(a)microsoft.com...

> Here is an example of one of the exposed methods (from the header):
>
> extern void standardCalc(double param(), double inputs(), double
> outputs());

Did you copy and paste that directly from the header? I don't claim to be a
C++ expert but I cannot see how that can possibly be valid syntax. Arrays
would be expressed as

extern void standardCalc(double[] param, double[] inputs, double[]
outputs);

I was just recently reading a book on C++ to knock the rust off my skills
because I was thinking of writing a program under Linux and I never saw
anything even remotely similar to the syntax you have above.


From: Peter Duniho on
jp2msft wrote:
> I've got a library from a government agency that only comes with the .lib and
> a header (.h) file.

As Jason says, you can't link to a static .lib from C#.

> The header shows me what methods are exposed, but I'm not sure if I can get
> to those features through a .Net Framework.
>
> If I want to call one of the four (4) exported methods of the DLL, how would
> I go about formatting the P/Invoke call to them?
>
> Here is an example of one of the exposed methods (from the header):
>
> extern void standardCalc(double param(), double inputs(), double outputs());

That does not look like correct C to me. Parens are used only for
function declarations and calls when paired with an identifier (they are
also, of course, used for certain program statements and expression
grouping).

Parameters used to pass arrays would use square brackets like C#, but
with the parameter name, not the type name. For example, "double param[]".

> I'm getting rather used to C# syntax, but I think "double param()" is an
> array of doubles - what I'd call "double[] param" in C#. If I am mistaken,
> please correct me!
>
> So, how would I go about invoking this exposed method from the library file
> "aesra10.lib"?

The first step is to get it into a DLL, which means wrapping the static
..lib in a DLL. One way to do that is to create a new empty C/C++ DLL
project, link the .lib into the project, and write a .def file that
exports the members of the .lib.

Alternatively, you can wrap each member of the .lib that you want to
expose in a simple wrapper that is exported from the DLL project
instead. This method is a little less efficient, but IMHO is preferable
because it allows you to use the declspec(dllexport) modifier in your
wrapper code rather than messing around with a .def file (I find
declaring exports in-code more intuitive than using a .def file…YMMV).

Either of those techniques should work fine as long as the original .lib
is plain C. If it's C++, then you'll have to wrap the C++ in some kind
of managed-code-accessible thing. The easiest way to do that is to just
build a mixed-mode managed C++ DLL (i.e. a managed C++ DLL that's not
"/clr:pure"). Then you can implement .NET types in the DLL that wrap
the unmanaged types from the .lib.

Having done that, the DLL will be usable just like any other .NET
managed DLL. One nice thing about this approach is that it gives you
the opportunity to wrap the original .lib in a more user-friendly
managed API, rather than forcing anyone using the library from managed
code to have to deal with the API from the .lib (which is almost
certainly not going to use .NET idioms and conventions).

You'll still have to do some marshaling yourself (see the Marshal class
and related attributes such as MarshalAsAttribute in the
System.Runtime.InteropServices namespace), but the C++ compiler knows
how to deal with simple things like strings, and provides a "pin_ptr"
type for locking down arrays and other managed objects so they can be
passed directly to unmanaged code conveniently.

> Also, all examples of P/Invoke only list the file name, not it's location.
> Do all files that I invoke this way have to be in a certain folder or can I
> direct it to the folder where my application will be storing this DLL?

P/invoke will need the DLL to be present either in the same directory
with the program, or in the DLL search path (mainly the System32
directory, if I recall correctly). I don't think you can change the DLL
search path programmatically, but I might be mistaken about that.

Pete
From: Doug Forster on
>
> That does not look like correct C to me. Parens are used only for
> function declarations and calls when paired with an identifier (they are
> also, of course, used for certain program statements and expression
> grouping).

It's perfectly correct C but it doesn't mean what the OP thinks. If that
really is the declaration then it means a pointer to a function with no
parameters that returns a double result.

Cheers
Doug Forster