Visual C# Developer Center > Visual C# Forums > Visual C# General > Is it possible to use win32 Classes in C#?
Ask a questionAsk a question
 

AnswerIs it possible to use win32 Classes in C#?

  • Monday, November 02, 2009 4:20 AMSieRV Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    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 having some attributes I need to set the value for those attributes ,
    . And i should made VssSdkServiceLogin obj to VssSdkMsg (it means Typecast) and call another function

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

Answers

All Replies

  • Monday, November 02, 2009 5:31 AMVic Vega Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     AnswerHas Code

    Hi,
     If they are pure C++ classes, then you need to write wrapper classes in MC++ to use C++ classes in C#. If they are COM classes, then they can be used directly.

    MC++ class

    //C++ classclass MyClass
    {
    }


    //MC++ class

    ref class MyClassWrapper
    {
    private:
     MyClass* nativeObj;
    }
    


    You can use MyClassWrapper in C#.

    I am not sure what you mean by VssSdkServiceLogin obj to VssSdkMsg. Are they related classes.

    Thanks
    PKR

  • Monday, November 02, 2009 6:15 AMGanesh Ranganathan - Bangalore, India Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    You can use Pinvoke to call the functions in unmanaged dlls. Use dumpbin.exe to find ou how these functions are exporerted so they can be called from .NET

    Here is an example for a WIN32 API call.

    http://pinvoke.net/default.aspx/kernel32/GetComputerName.html
    Ganesh Ranganathan
    [Please mark the post as answer if it answers your question]
    blog.ganeshzone.net
  • Wednesday, November 04, 2009 5:30 AMRoahn LuoMSFT, ModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Hello SieRV,

    I agree with Ganesh and Vic, they are both excellent suggestion. There are three choices for us when we are doing Managed-Native Interop: P/Invoke, COM Interop and C++/CLI technology. Each has its advantages and disadvantages. The choice depends on our native APIs. Basically, for managed access to C-style APIs, we could use P/Invoke, but we need to wrap each API seperately; If you want to consume COM interfaces from managed code, then COM interop is recommend, it is a good choice if you are already using COM; while C++/CLI tenology use the managed C++ to be the bridge of native world and the managed world.

    Please take a look at this article, I believe it could help you:
    http://msdn.microsoft.com/en-us/magazine/dd315414.aspx

    There is a sample for P/Invoke technology in the All-In-One CodeFx site, you could download it and take a look at the detailed steps:
    http://cfx.codeplex.com/Release/ProjectReleases.aspx?ReleaseId=24055

    Best regards,
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    If you have any feedback, please tell us.
    Welcome to the All-In-One Code Framework!