简体   繁体   中英

Properly exposing functions in VB class library using c#

I have to communicate with a controller that uses an unmanaged DLL in C#. The documentation isn't very helpful for the DLL and I have no experience with talking to DLL's. The company does provide a sample VB project with a class that wraps the DLL from VB. The class is called ctccom32v2.

I thought that since the dirty work of calling the unmanaged DLL is already done including structs and other variables, I could use that class to create a VB class library. I figure it will save me a lot of time and effort. So I added that class source file to a VB class library project and it spat out a dll when built. I then added that dll to my C# project references and I am able to see the functions in the reference browser. The library I created is named CTC_Lib.

(if your wondering why I just don't write my program in VB and use their class, I prefer and I am more comfortable working in C#)

The problem I am running into is this: If I create an instance of the library using

CTC_Lib.Ctccom32v2 ctc = new CTC_Lib.Ctccom32v2();

and then attempt to type ctc.somefunction, intellisense shows a few default methods like Equals, GetHashCode, etc. I do not see any of the Ctccom32v2 functions which expose the unmanaged DLL.

if I type the library and class name manually like so:

CTC_Lib.Ctccom32v2.

The intellisense list pops up with all of the functions in Ctccom32v2.

If I add another class to the VB class library (Lets call it "somelib") and stick a simple function into it:

Public Function add() as Void
Return 1+2;
End Function

I then use the same method for creating an instance:

CTC_Lib.somelib ctc = new CTC_Lib.somelib();

the function "add" now pops up in the intellisense window by simply typing ctc.

Could it because the way the functions are declared in the VB class? Here is one of MANY functions in the VB class, they are all declared the same using "Declare Auto Function":

Declare Auto Function CtOpenConnection Lib "Ctccom32v2.dll" _
    (ByVal ConnectID As Integer, ByVal CommPort As Integer, ByVal Address As Integer) As Integer

I don't know what Auto function means but that does not expose the function when you attempt to create an instance of the class it resides in.

If this is or is not the proper way to call those functions within the VB class library please let me know, I am a novice. Also, forgive me if some of my terminology is incorrect; I am not good with programming jargon (yet).

The intellisense differs in your two scenarios because the unmanaged DLL lacks the required metadata to properly display the information about the functions. Your VB function does possess that metadata.

You already know the workaround; type the library and class name manually. Alternatively, you can wrap the unmanaged functions in managed VB methods, which will provide the needed metadata for intellisensing.

See the MSDN documentation on the Declare statement . This looks similar to declaring an extern method in C#. I think what you're missing is the access modifier. It should be like Public Declare ... to allow it to be used outside of where it's declared.

If that wasn't the issue, you might want to declare it as a Public Shared Function in a public class, using DllImport . An example of that is shown at the bottom of the Declare statement doc .

It can also be useful, maybe for this, and for C#/VB conversions in general, to decompile your assembly in ILSpy and see what is generated due to these lines.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM