none
Using VC++.NET dll in C#.NET RRS feed

  • Question

  • Hi,

    I am siddireddy, can anybody please help in calling VC++.NET dll methods in C#.NET application. i made all the methods as public in the class. I added the dll in the references of the application and i can see the class name but not the method names.

     

    Thanks in advance,

    Siddireddy

    Thursday, April 17, 2008 3:21 PM

Answers

  • Hi,

       According to your description, I know you want to use the managed Visual C++ dll in Visual C# application. 

     

       I have written a demo to test the result, and firstly I create a Visual C++ Class Library project named MyManagedDLL. 

       Here's the codes:

     

    Code Snippet

    // MyManagedDLL.h

    #pragma once

    #include "stdlib.h"
    using namespace System;

    namespace MyManagedDLL {

     public ref class MyClass
     {
      // TODO: Add your methods for this class here.
      public:
       int
    RandomValue()  //The methods export.
       { 
        return rand();
       }
     };
    }

     

    Build the project and then create a Visual C# application and add the managed Visual C++ dll into the C# project reference.  Then use the method in the dll.

    Here's the code in C# application:

     

    Code Snippet

    using MyManagedDLL;
    public partial class Form1 : Form
    {
         public Form1()
         {
             InitializeComponent();

     

             //Use the methods RandomValue in the Class MyClass

             MyClass cl = new MyClass();   

             int num = cl.RandomValue();

             MessageBox.Show(num.ToString());

         }

    }

     

    Base on my test, these works, and if you want to see the methods in the class when just type the class name, like in my instance, MyClass, you can define some static methods in Visual C++ dll. 

     

    Hope these can help you.

     

    Best Regards,

     

    Fat-ZhiZhi

    Friday, April 18, 2008 8:22 AM
  • Hi,

    for this you can import the dll's created in C++ with the C# application.

    use the namespace

    using System.Runtime.InteropServices;

    and can import the dll containing function using:

    [DllImport("User32.dll")]
    public static extern bool dllFunction();

    and can use this function in the exsisting C# application.

    Hope this helps!!
    Friday, April 18, 2008 1:59 PM

All replies

  • Hi,

       According to your description, I know you want to use the managed Visual C++ dll in Visual C# application. 

     

       I have written a demo to test the result, and firstly I create a Visual C++ Class Library project named MyManagedDLL. 

       Here's the codes:

     

    Code Snippet

    // MyManagedDLL.h

    #pragma once

    #include "stdlib.h"
    using namespace System;

    namespace MyManagedDLL {

     public ref class MyClass
     {
      // TODO: Add your methods for this class here.
      public:
       int
    RandomValue()  //The methods export.
       { 
        return rand();
       }
     };
    }

     

    Build the project and then create a Visual C# application and add the managed Visual C++ dll into the C# project reference.  Then use the method in the dll.

    Here's the code in C# application:

     

    Code Snippet

    using MyManagedDLL;
    public partial class Form1 : Form
    {
         public Form1()
         {
             InitializeComponent();

     

             //Use the methods RandomValue in the Class MyClass

             MyClass cl = new MyClass();   

             int num = cl.RandomValue();

             MessageBox.Show(num.ToString());

         }

    }

     

    Base on my test, these works, and if you want to see the methods in the class when just type the class name, like in my instance, MyClass, you can define some static methods in Visual C++ dll. 

     

    Hope these can help you.

     

    Best Regards,

     

    Fat-ZhiZhi

    Friday, April 18, 2008 8:22 AM
  • Hi,

    for this you can import the dll's created in C++ with the C# application.

    use the namespace

    using System.Runtime.InteropServices;

    and can import the dll containing function using:

    [DllImport("User32.dll")]
    public static extern bool dllFunction();

    and can use this function in the exsisting C# application.

    Hope this helps!!
    Friday, April 18, 2008 1:59 PM
  • Hi Fat-ZhiZhi  'n' Sunil Soni,

    Thank u for the reply. can please give me the link for Using P/Invoke, so that i can know which type i can use for converting like P/Invoke..............

     

    Once again thanks a lot.


    Wednesday, May 7, 2008 12:28 PM