0

I have a C-DLL with a short documentation and I'd like to use this DLL in my C# program.

Unfortunately the documentation is for an Excel-Makro which is password-protected and so I don't know the exact function names and parameter types.

I used DependencyWalker to find all export functions of the DLL and together with the documentation I figured out that the documented function ptx is called FU_PTX in the DLL and expects one parameter and has a return value. Unfortuntely I still don't know the type of the parameter or the return value but I know that it is a number.

So I wrote following code:

...
        [DllImport("dmata.dll")]
        public static extern UInt32 FU_PTX(UInt32 x);

        FU_PTX(11); 
...

This code throws an AccessViolationException. I also tryed other types as int, long, double but I always get the same exception.

As far as I know there is no way to get the required types from the dll directly but perhaps anyone has an idea what might be wrong or can point me in the right direction.


Edit:

I managed to get the signatures from the vba-file:

Private Declare Function FU_PTX# Lib "dmata.dll" (FT#)

I ported this to following C# code:

[DllImport("dmata.dll")]
public static extern double FU_PTX(double x);


FU_PTX(1.5);

I still receive the same AccessViolationException. Anyone an idea why I still get the same exception?

user1567896
  • 2,398
  • 2
  • 26
  • 43
  • 1
    If you don't know the function's signature, calling it successfully is close to hopeless -- your best shot really is to get that. For starters, the parameter could be a pointer to a structure with particular fields (this is true even if it's "a number" -- it could be a `VARIANT`), or the calling convention could be `cdecl` rather than `stdcall`. See if you can get a hold of a header file (.h) declaring the function's prototype. If someone wrote this DLL with the intention of it getting used, there must be documentation on the signatures *somewhere*. – Jeroen Mostert Feb 14 '15 at 11:49
  • If anyone asks I deny everything but [is this helpful](http://stackoverflow.com/a/7835861/578411)? – rene Feb 14 '15 at 13:43
  • @Jeroen Mostert: Thanks for your comment. It is a very old DLL intended for the use in Excel. The documentation only states the function names and input/output parameters but without their types. The DLL does some complex math functions and I'd like to implement a better user experience in C# than in Excel. I don't think I'll have a chance to get the header file or the signatures. – user1567896 Feb 14 '15 at 13:47
  • If the DLL is used from Excel, that means it's intended to be called from VBA. If you've succeeded in removing the password protection and have the VBA code that's calling this function, you can reverse engineer the calls from that. Add it to your question if you need help with it. – Jeroen Mostert Feb 14 '15 at 14:54
  • Yes, I'll try to remove the password on monday as I have the files only at work. When I succeed in removing the password it should be no problem to port the calls to C#. Thanks for your help. – user1567896 Feb 14 '15 at 15:47
  • @JeroenMostert I updated my post. Unfortunately it does not work. Any idea why it is not working? – user1567896 Feb 16 '15 at 11:27
  • Added as an answer. Let me know if that still doesn't work. – Jeroen Mostert Feb 16 '15 at 14:05

1 Answers1

2

This syntax:

Private Declare Function FU_PTX# Lib "dmata.dll" (FT#)

Is rather obscure with its use of the # suffix; in "modern" VBA (if there can be said to be such a thing) the declaration would be

Private Declare Function FU_PTX Lib "dmata.dll" (ByRef FT As Double) As Double

Note the unfortunate default of ByRef; arguments are passed by reference in VBA by default (even if the function has no intention of modifying them).

This should correspond with the following C# declaration:

[DllImport("dmata.dll")]
public static extern double FU_PTX(ref double FT);

Since the argument is passed by reference, you can't pass a constant and must always use a variable:

double ft = 11.0;
FU_PTX(ref ft);
Jeroen Mostert
  • 27,176
  • 2
  • 52
  • 85