szaranger
asked on
Accessing C++ DLL from C#
I'm trying to access functions from a C++ DLL from C#. I neither can reference nor register the dll. Is it possible for me to access the functions within C#? But it's possible to access the DLL from VB6.
// part of my code is as below:
[StructLayout(LayoutKind.Sequential)]
public struct DataPointValues
{
[MarshalAs(UnmanagedType.LPStr, SizeConst = 64)]
public string pointName;
[MarshalAs(UnmanagedType.LPStr, SizeConst = 64)]
public string paramName;
public int paramOffset;
[MarshalAs(UnmanagedType.I2, SizeConst = 1)]
public int paramType;
public int paramValue;
}
[DllImport("hscnetapi.dll", EntryPoint = "rHsc_Param_Value_Put_Bynames")]
public static extern long rHsc_Param_Value_Put_Bynames(string server, int num_requests, DataPointValues param_byname_data_array);
ASKER
Is it possible to access an unmanaged DLL without regitsering it from C#?
What kind of DLL are you talking about? If it is a COM DLL, then it needs to be registered. Otherwise, you should be able to access it with the right p/invoke signature.
Do you have the signature from the C++ DLL, or is it someone else's DLL?
Bob
Do you have the signature from the C++ DLL, or is it someone else's DLL?
Bob
ASKER
It is third party COM DLL. The signature is as below:
function rHsc_Param_Value_Puts(Serv er: LPSTR; num_requests: INT; out param_value_data_array: ^SafeArray): INT; stdcall;
function rHsc_Param_Value_Puts(Serv
For 'out' parameters, you need the 'ref' keyword:
...ref DataPointValues param_byname_data_array
The SafeArray is trickier, and I believe that you might be able to use the VariantWrapper to help out:
VariantWrapper Class:
http://msdn2.microsoft.com/en-us/library/system.runtime.interopservices.variantwrapper(vs.80).aspx
Bob
...ref DataPointValues param_byname_data_array
The SafeArray is trickier, and I believe that you might be able to use the VariantWrapper to help out:
VariantWrapper Class:
http://msdn2.microsoft.com/en-us/library/system.runtime.interopservices.variantwrapper(vs.80).aspx
Bob
ASKER
When I run the code, it returns me this error:
Unable to find an entry point named 'rHsc_Param_Value_Put_Byna
[StructLayout(LayoutKind.Sequential)]
public struct DataPointValues
{
[MarshalAs(UnmanagedType.LPStr, SizeConst = 64)]
public string pointName;
[MarshalAs(UnmanagedType.LPStr, SizeConst = 64)]
public string paramName;
public int paramOffset;
[MarshalAs(UnmanagedType.I2, SizeConst = 1)]
public int paramType;
public int paramValue;
}
[DllImport("hscnetapi.dll", EntryPoint = "rHsc_Param_Value_Put_Bynames")]
public static extern long rHsc_Param_Value_Put_Bynames(string server, int num_requests, ref DataPointValues param_byname_data_array);
You can use dumpbin (or dependency walker) to view the exports for the DLL to make sure about the entry point name. It is also possible to use the 'unsafe' keyword and pointers with C# p/invoke signatures.
Example:
Using Unsafe Code in C#
http://www.csharpfriends.com/Articles/getArticle.aspx?articleID=351
Bob
Example:
Using Unsafe Code in C#
http://www.csharpfriends.com/Articles/getArticle.aspx?articleID=351
Bob
ASKER
Dependancy walker returns:
rhsc_param_value_put_bynam es as the Function and
A hexadecimal value as a Entry Point
So it is the same name as what I use.
rhsc_param_value_put_bynam
A hexadecimal value as a Entry Point
So it is the same name as what I use.
ASKER
Apparently .NET cannot access a COM DLL without registering it!!!???
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks Bob.
Bob