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.

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.


Comments

  1. Morris

    • 2016/10/25

    I have win32 DLL named VssSdkd.dll. It contains two classes with names VssSdkServiceLogin and VssSdkMsg. In C#, I need to import the 

  2. Kenneth

    • 2019/6/19

    6. 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.

  3. Ari

    • 2020/7/11

    Win32 API Tutorial: DLL, MFC, A dynamic-link library (DLL) is an executable file that acts as a shared library of functions. Dynamic linking provides a way 

  4. Jayden

    • 2018/12/26

    Calling Win32 DLLs in C# with P/Invoke. Jason Clark. Code download available at:NET0307.exe(133 KB) Contents. Enter P/Invoke Style The DLL Import Attribute Optional DllImportAttribute Properties Data Marshaling Marshaling Numerical and Logical Scalars Parameters that are Pointers Marshaling Opaque Pointers: a Special Case Marshaling Text Summing it Up

  5. Stone

    • 2018/4/15

    On Win32 platforms, the compiler is capable of both producing and using There are two distinct ways in which DLLs can be used: Using from C++.

  6. Alan

    • 2021/6/16

    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

  7. Clark

    • 2017/10/21

    NET or any kind of code in these DLLs, but if you want to be able to use them across all Windows platforms, make sure your code is C++ compliant code or 

  8. Talon

    • 2018/10/8

    It depends. Win32 dlls will not work though. Wine (originally an acronym for "Wine Is Not an Emulator") is a compatibility layer capable of running Windows applications on several POSIX-compliant operating systems, such as Linux, Mac OSX, & BSD. Instead of simulating internal Windows logic like a virtual machine or emulator, Wine translates

  9. Raul

    • 2019/6/5

    environments, you should be able to use almost any development can call DLLs that use the stdcall calling convention as well as DLLs that use C calling.

  10. Hugh

    • 2019/2/19

    Using Visual Studio, you can build Win32 DLLs in C or C++ that do not use the Microsoft Foundation Class (MFC) library. You can create a non-MFC DLL project with the Win32 Application Wizard. The MFC library itself is available, in either static link libraries or in a number of DLLs, with the MFC DLL Wizard.

  11. Cody

    • 2019/9/14

    c and mapparser.h) normally generated on unix, so if possible, using a nightly snaphot is substantially easier than working directly from GitHub 

  12. Jeffery

    • 2021/6/26

    Hi, We need to write a plugin dll to integrate with a 'legacy' win 32 application using C++. Sample files are in Dev Studio 6 C++, but we would like to use Visual C++ if it

  13. Troy

    • 2016/6/1

    If you have code written in C, C++, or another programming language, you can utilize it in LabVIEW by calling the Dynamically Linked Library 

  14. Matthias

    • 2016/9/21

    Use the -shared switch to tell the compiler that the generated code is to be put into a DLL. Code compiled for an EXE file will use the optimization assumption that _tls_index==0. Such code in a DLL will crash. DLLs with a C Interface. A DLL presenting a C interface can connect to any other code in a language that supports calling C functions

  15. Leonidas

    • 2018/3/16

    Using the LoadLibrary and GetProcAddress functions from the Windows LibLoader API it is possible to dynamically-link DLL files to your 

  16. Westin

    • 2019/2/5

    DLL. However, C/C++ programs using UCRTBASE.DLL are forced to link against another new DLL, the VCRuntime, whose name continues to change with each version 

Comments are closed.

Recent Posts