简体   繁体   中英

Is it possible to use win32 DLLs in C#?

I have win32 DLL named VssSdkd.dll. It contains two classes with names VssSdkServiceLogin and VssSdkMsg.

I need to import the VssDskServiceLogin class, in C#. I am setting few properties of VssDskServiceLogin object and passing this to VssSdkMsg which in turn invokes some other method.

How can I achieve this using C#.

I have win32 DLL named VssSdkd.dll. It contains two classes with names VssSdkServiceLogin and VssSdkMsg.

In C#, I need to import the VssDskServiceLogin class. In the class are some attributes I need to set the value for, sending the information to VssSdkMsg and call another function

I need to achieve those things through C# code. Is this possible, and if so, how?

Classes compiled in C++ (and other Win32 languages) cannot be interoped with Dot NET languages. Structures may be if care is taken. Dot NET does have support for COM objects, though.

Native functions may be called from Dot NET languages if they're tagged with the [DllImport] attribute on the CLR side (and the appropriate DllImportAttribute properties are set) - and exported on the Win32 side. However, this is a non-trivial process. I would recommend grabbing a good book on the subject and starting from the top. SO is probably not a very good medium for addressing this issue.

You can do it with p/invoke and marshaling. Read about it, it's too complicated a subject to explain fully in a SO answer.

I believe it is sometimes possible in C# through P/Invoke, but when dealing with classes, this can get very, very difficult.

I'd highly recommend creating a C# wrapper for your DLL using Managed C++ instead.

I don't know that this link will solve your problem directly, but I expect you can find a good example of how to do this at Code Project . Managed C++ can be a little tricky until you get used to it, and I think the syntax changed from .NET 1.0/1.1 to .NET 2.0. Make sure you know what version of .NET you're targeting and search the Code Project site accordingly.

Possibly what you are looking for is interoperability with COM. I haven't worked much on it, but can give you a sample code to start with.

[DllImport("user32.dll")]
private static extern int MessageBox(IntPtr hWnd, String
text, String caption, uint type);

static void Main(string[] args)
{
MessageBox(new IntPtr(0), "Hello, world!", "My box", 0);
}

This may be of help.

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