Calling function from .NET DLL


banv2000

Recommended Posts

Hello,

I have a DLL, written in C#, it has some function that I want to call from DAQFactory:

public static string myFunc(string p1, string p2){}

And I put it int system32 folder, then I add following code in main sequence (auto startup)

extern("my.dll", "string myFunc(string, string)","myFunc2","stdcall")

But I can not call my function, DAQFactory throw an exception (Channel or function not found: .... Unable to perform quick sequence action), anything wrong with my code?

Link to comment
Share on other sites

Its giving you that error because it can't find the function. You can't just access arbitrary functions inside of DLL's. The DLL has to explicitly expose the function. I'm not familiar with C# so can't give you any input on how to do it in C#. In C++ its usually done through __declspec and other similar specifiers and also under an extern "C".

Which brings up another point: even if you did expose myFunc(), "string" in C# is probably a C# object, not a simple array of bytes. This is quite typical of modern languages. Strings are usually encapsulated inside of some sort of object to handle all the convenient things like resizing, concatenation and the like. However, DAQFactory isn't written in C# and so has no idea what a "string" is to C#. When you use "string" in the declaration in the extern() it thinks its just an array of bytes. It also requires [x] after it so that it knows how long the string is, where x is the maximum number of characters in the string.

Link to comment
Share on other sites

  • 2 weeks later...

Now I use a DLL written in C++, i have follwing function in header file:

int _stdcall myFunc(unsigned char data[], unsigned int iLen, unsigned int iStart, unsigned char *ret);

An I can call it from Visual Basic.

In DAQFactory, I declare it as follow:

extern("my.dll", "ushort myFunc(string, ushort, ushort, string[160])","myFunc","stdcall")

extern("my.dll", "ushort getVersion()", "getVersion","stdcall")

But "myFunc" does not work ("getVersion" working properly), can you help me?

Regards,

Link to comment
Share on other sites

That's because unsigned char data[] is still a pointer. Its really the same thing as unsigned char *. So you need to tell DAQFactory how big the string is (like you did for the second one). Also your return value is wrong. You are returning an int, but telling DF its a unsigned int. Probably won't matter since they are the same size, but still, you have to be very precise and accurate when doing DLL's with any language.

Link to comment
Share on other sites

Thanks for quick reply, but i still got no luck with that function.

I've re-declare as following in DF:

extern("my.dll", "short myFunc(string[160], ushort, ushort, string[160])","myFunc","stdcall")

And later in another sequence:

private String data = "something"

private String returnData=""

private i2=0

private v = GetLength(data)

private i = myFunc(@data, v, i2, @returnData)

return returnData

Anything wrong with my code?

Link to comment
Share on other sites

Well, first, "int" means different things to different compilers. I recommending using "short" or "long" in your C++ code.

Second, I would avoid the

unsigned char data[]

notation and, assuming you are not changing this value, make it:

const char *

Next, do you have your C declaration inside an extern "C" {} block? If not, then the C++ compiler is going to name mangle the function and DAQFactory won't be able to find it.

Finally, are you actually exporting this function? Either through a .def file or a using declspec?

I would recommend downloading depends.exe and running it on your DLL so you can see how your function appears (and if it even does).

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.