Solved

Accessing C++ DLL from C#

Posted on 2007-11-26
11
2,383 Views
Last Modified: 2013-12-16
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);

Open in new window

0
Comment
Question by:szaranger
  • 6
  • 5
11 Comments
 
LVL 96

Expert Comment

by:Bob Learned
ID: 20354399
There is a huge difference between VB6 and VB.NET/C# when it comes to interfacing with C++ DLLs, especially when it comes to string marshalling.  You need to find juse the right combination of attributes.  It would help if you had the C++ prototype declaration to work from.

Bob
0
 
LVL 2

Author Comment

by:szaranger
ID: 20354436
Is it possible to access an unmanaged DLL without regitsering it from C#?
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 20354464
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
0
 
LVL 2

Author Comment

by:szaranger
ID: 20354486
It is third party COM DLL. The signature is as below:

function rHsc_Param_Value_Puts(Server: LPSTR; num_requests: INT; out param_value_data_array: ^SafeArray): INT; stdcall;
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 20354531
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
0
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
LVL 2

Author Comment

by:szaranger
ID: 20354556

When I run the code, it returns me this error:
Unable to find an entry point named 'rHsc_Param_Value_Put_Bynames' in DLL 'hscnetapi.dll'.
[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);

Open in new window

0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 20354573
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
0
 
LVL 2

Author Comment

by:szaranger
ID: 20354625
Dependancy walker returns:
     rhsc_param_value_put_bynames        as the Function and
      A hexadecimal value as a Entry Point
So it is the same name as what I use.


0
 
LVL 2

Author Comment

by:szaranger
ID: 20355445
Apparently .NET cannot access a COM DLL without registering it!!!???
0
 
LVL 96

Accepted Solution

by:
Bob Learned earned 250 total points
ID: 20357690
Hmmm, let's revisit a previous comment:

"If it is a COM DLL, then it needs to be registered".

Bob
0
 
LVL 2

Author Closing Comment

by:szaranger
ID: 31411106
Thanks Bob.
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
asp.net mvc5 3 38
Need to Modify a Script I found 5 77
Get list of word ducuments in a folder 10 18
Birthdays 3 14
In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now