createdll tutorial VC++ 2005

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Eran.Yasso@gmail.com

    createdll tutorial VC++ 2005

    Hi,

    I need to develop DLL that exposes API. It should be accessed by C#.
    Can any one please help me with that? any tutorial or link would be
    greate.

    TIA,

  • Imm

    #2
    Re: createdll tutorial VC++ 2005


    """Eran.Yasso@g mail.com ÐÉÓÁÌ(Á):
    """
    Hi,
    >
    I need to develop DLL that exposes API. It should be accessed by C#.
    Can any one please help me with that? any tutorial or link would be
    greate.
    >
    TIA,
    Create dll in Visual C++ 2005:

    1. "New Project\Win32\W in32 Console Application" create a new project.
    -OK
    2. "Applicatio n Settings\ Empty project" -FINISH
    3. In "Solution Explorer" on "Source files" add new item "main.cpp"
    4. Copy the text:

    #include <windows.h>
    #include <cstring>

    int __declspec(dlle xport) FuncA(int i)
    {
    return i*10;
    };
    int __declspec(dlle xport) FuncB(int i)
    {
    return i*100;
    };
    char userLogin[80], userPassword[80];
    bool __declspec(dlle xport) camomileLogin(c har user_name[], char
    user_password[])
    {

    strcpy(userLogi n,user_name);
    strcpy(userPass word,user_passw ord);
    return true;
    };
    __declspec(dlle xport) char* camomileGetUser Login()
    {
    return userLogin;
    };

    5. In "Property Pages\Configura tion Properties\Gene ral\Configurati on
    Type" select "Dynamic Library (.dll)" APPLY
    6. Build.

    You may use the tool ""C:\Progra m Files\Microsoft Visual Studio
    8\Common7\Tools \Bin\Depends.Ex e""

    Code for C# :

    using System;
    using System.Collecti ons.Generic;
    using System.Componen tModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows. Forms;
    using System.Runtime. InteropServices ;

    namespace camomile_root
    {
    public partial class Form1 : Form
    {
    [DllImport("libc amomile.dll", EntryPoint = "?FuncA@@YAHH@Z ")]
    public static extern int FuncA(int x);
    [DllImport("libc amomile.dll", EntryPoint = "?FuncB@@YAHH@Z ")]
    public static extern int FuncB(int x);
    [DllImport("libc amomile.dll", EntryPoint =
    "?camomileGetUs erLogin@@YAPADX Z")]
    public static extern string camomileGetUser Login();
    [DllImport("libc amomile.dll", EntryPoint =
    "?camomileLogin @@YA_NQAD0@Z", CharSet = CharSet.Ansi, CallingConventi on
    = CallingConventi on.StdCall)]
    public static extern bool camomileLogin(s tring user_name,
    string user_password);

    public Form1()
    {
    InitializeCompo nent();
    }

    private void okey_Click(obje ct sender, EventArgs e)
    {
    int answer = FuncA(57);
    userLogin.Text = answer.ToString ();
    int answer1 = FuncB(57);
    userPassword.Te xt = answer1.ToStrin g();
    string s = "user";
    string s2 = "passwd";
    camomileLogin(s ,s2);
    string answer2 = camomileGetUser Login();
    userPassword.Te xt = answer2;

    }

    private void cancelButton_Cl ick(object sender, EventArgs e)
    {
    Close();
    }
    }
    }

    Comment

    • Bruno van Dooren [MVP VC++]

      #3
      Re: createdll tutorial VC++ 2005

      Not an ideal solution, because you have to import mangled names, which can
      be annoying to figure out, and the mangled names change if you add a funcion
      parameter.
      A better method is so insure that the function names are not mangled.
      The easiest ways to do this are either change
      int __declspec(dlle xport) FuncA(int i)
      to

      extern "c"
      {
      __declspec(dlle xport) int __cdecl FuncA(int i);
      }

      int FuncA(int i)
      {
      //..implementatio n here
      }
      or add a DEF file to your project and declare function exports in there.
      that way you can also export unmangled functions that use the stdcall
      calling convention.
      [DllImport("libc amomile.dll", EntryPoint = "?FuncA@@YAHH@Z ")]
      public static extern int FuncA(int x);

      Do not foget to use the CallingConventi on attribute to specify the calling
      convention that was used. otherwise there will be an access violation.

      --

      Kind regards,
      Bruno van Dooren
      bruno_nos_pam_v an_dooren@hotma il.com
      Remove only "_nos_pam"


      Comment

      • Rich

        #4
        Re: createdll tutorial VC++ 2005

        Eran.Yasso@gmai l.com wrote:
        Hi,
        >
        I need to develop DLL that exposes API. It should be accessed by C#.
        Can any one please help me with that? any tutorial or link would be
        greate.
        >
        TIA,
        Here is a link to a good tutorial on setting up a project to build an
        unmanaged DLL:


        I followed this article and created a very simple test DLL (using a
        ..DEF file for exporting functions).


        Here is the C++ file:

        #include <iostream>
        #include "Test_DLL.h "

        #ifdef __cplusplus
        extern "C" {
        #endif

        int Add(int a, int b) {
        return( a + b );
        }

        void Function( void ) {
        std::cout << "DLL Called!" << std::endl;
        }

        #ifdef __cplusplus
        }
        #endif


        Here is the Header file:

        #ifndef _DLL_TUTORIAL_H _
        #define _DLL_TUTORIAL_H _
        #include <iostream>

        #ifdef __cplusplus
        extern "C" {
        #endif

        int Add(int a, int b);
        void Function( void );

        #ifdef __cplusplus
        }
        #endif

        #endif


        Here is the DEF file:

        LIBRARY Test_DLL
        EXPORTS
        Add @1
        Function @2



        Once you have your unmanaged DLL, test it from an unmanaged application
        to make sure everything works.
        This builds to a .DLL file and a .LIB file. We need these two files
        plus the header file for an application to use the DLL.
        Here is a simple test program that tests our DLL:
        #include <iostream>
        #include "Test_DLL.h "

        int main()
        {
        Function();
        std::cout << Add(32, 58) << "\n";
        return(1);
        }



        Now you need to build a MANAGED DLL (assembly) so you can call your
        functions from C# (or VB or any other .NET language).
        Here is the managed C++ file for the managed DLL:

        using namespace System::Runtime ::InteropServic es;

        [DllImport("Test _DLL", EntryPoint="Add ")]
        extern "C" int Add(int a, int b);

        [DllImport("Test _DLL", EntryPoint="Fun ction")]
        extern "C" void Function( void );

        namespace Test_DLL_CLR {

        public ref class Test_DLL_Class {
        public:
        // Function: Add
        int Add_clr(int a, int b)
        {
        return( Add( a, b) );
        }

        // Function: Function
        void Function_clr(vo id)
        {
        Function();
        }
        };
        }


        Once you have your managed DLL, you can include it in your C# project
        as a reference. You should now be able to use the namespace defined in
        the managed DLL (along with everything defined in that namespace).
        Here is C# code that uses the managed DLL:

        using System;
        using System.Collecti ons.Generic;
        using System.Text;
        using Test_DLL_CLR;

        namespace Test_DLL_CLR_ap p_cs
        {
        class Program
        {
        static void Main(string[] args)
        {
        Test_DLL_Class myclass = new Test_DLL_Class( );
        int val;

        Console.WriteLi ne("C# Test_DLL_CLR Test Program");

        myclass.Functio n_clr();
        val = myclass.Add_clr (123, 456);

        Console.WriteLi ne("val = " + val);

        Console.WriteLi ne("Finished.") ;
        }
        }
        }

        Comment

        • Ben Voigt

          #5
          Re: createdll tutorial VC++ 2005


          <Eran.Yasso@gma il.comwrote in message
          news:1164315840 .407737.226460@ f16g2000cwb.goo glegroups.com.. .
          Hi,
          >
          I need to develop DLL that exposes API. It should be accessed by C#.
          Does it need to be used from any non-.NET language?
          Can any one please help me with that? any tutorial or link would be
          greate.
          building a C++/CLI assembly for consumption by C# is trivially easy, just do
          new project -C++ -CLR -Class Library, then add some "ref class"
          definitions (there's a wizard for that too, right click the C++ project and
          do Add -New Class).
          >
          TIA,
          >

          Comment

          • Eran.Yasso@gmail.com

            #6
            Re: createdll tutorial VC++ 2005


            Imm wrote:
            """Eran.Yasso@g mail.com ÐÉÓÁÌ(Á):
            """
            Hi,

            I need to develop DLL that exposes API. It should be accessed by C#.
            Can any one please help me with that? any tutorial or link would be
            greate.

            TIA,
            >
            Create dll in Visual C++ 2005:
            >
            1. "New Project\Win32\W in32 Console Application" create a new project.
            -OK
            2. "Applicatio n Settings\ Empty project" -FINISH
            3. In "Solution Explorer" on "Source files" add new item "main.cpp"
            4. Copy the text:
            >
            #include <windows.h>
            #include <cstring>
            >
            int __declspec(dlle xport) FuncA(int i)
            {
            return i*10;
            };
            int __declspec(dlle xport) FuncB(int i)
            {
            return i*100;
            };
            char userLogin[80], userPassword[80];
            bool __declspec(dlle xport) camomileLogin(c har user_name[], char
            user_password[])
            {
            >
            strcpy(userLogi n,user_name);
            strcpy(userPass word,user_passw ord);
            return true;
            };
            __declspec(dlle xport) char* camomileGetUser Login()
            {
            return userLogin;
            };
            >
            5. In "Property Pages\Configura tion Properties\Gene ral\Configurati on
            Type" select "Dynamic Library (.dll)" APPLY
            6. Build.
            >
            You may use the tool ""C:\Progra m Files\Microsoft Visual Studio
            8\Common7\Tools \Bin\Depends.Ex e""
            >
            Code for C# :
            >
            using System;
            using System.Collecti ons.Generic;
            using System.Componen tModel;
            using System.Data;
            using System.Drawing;
            using System.Text;
            using System.Windows. Forms;
            using System.Runtime. InteropServices ;
            >
            namespace camomile_root
            {
            public partial class Form1 : Form
            {
            [DllImport("libc amomile.dll", EntryPoint = "?FuncA@@YAHH@Z ")]
            public static extern int FuncA(int x);
            [DllImport("libc amomile.dll", EntryPoint = "?FuncB@@YAHH@Z ")]
            public static extern int FuncB(int x);
            [DllImport("libc amomile.dll", EntryPoint =
            "?camomileGetUs erLogin@@YAPADX Z")]
            public static extern string camomileGetUser Login();
            [DllImport("libc amomile.dll", EntryPoint =
            "?camomileLogin @@YA_NQAD0@Z", CharSet = CharSet.Ansi, CallingConventi on
            = CallingConventi on.StdCall)]
            public static extern bool camomileLogin(s tring user_name,
            string user_password);
            >
            public Form1()
            {
            InitializeCompo nent();
            }
            >
            private void okey_Click(obje ct sender, EventArgs e)
            {
            int answer = FuncA(57);
            userLogin.Text = answer.ToString ();
            int answer1 = FuncB(57);
            userPassword.Te xt = answer1.ToStrin g();
            string s = "user";
            string s2 = "passwd";
            camomileLogin(s ,s2);
            string answer2 = camomileGetUser Login();
            userPassword.Te xt = answer2;
            >
            }
            >
            private void cancelButton_Cl ick(object sender, EventArgs e)
            {
            Close();
            }
            }
            }
            Hi All and thanks for your reply,

            Imm,I have question regarding your answer.

            I changed the functions name in the dll. when i changed the Entry
            points in C#, with the same name, I got massage that the entry points
            can't be found. I changed function FuncA to GetPosition and FuncB to
            SetPosition. I also changed in C#:
            [DllImport("Yass o.dll", EntryPoint = "?FuncA@@YAHH@Z ")]
            public static extern int FuncA(int x);
            to
            [DllImport("Yass o.dll", EntryPoint = "?GetPosition@@ YAHH@Z")]
            public static extern int GetPosition(int x);

            Why is it not working?
            TIA,

            Comment

            • Imm

              #7
              Re: createdll tutorial VC++ 2005

              Hi All and thanks for your reply,
              >
              Imm,I have question regarding your answer.
              >
              I changed the functions name in the dll. when i changed the Entry
              points in C#, with the same name, I got massage that the entry points
              can't be found. I changed function FuncA to GetPosition and FuncB to
              SetPosition. I also changed in C#:
              [DllImport("Yass o.dll", EntryPoint = "?FuncA@@YAHH@Z ")]
              public static extern int FuncA(int x);
              to
              [DllImport("Yass o.dll", EntryPoint = "?GetPosition@@ YAHH@Z")]
              public static extern int GetPosition(int x);
              >
              Why is it not working?
              TIA,
              I have not problem. You mayby don't rewrite your new version dll and
              you C# see old version.

              See my source code. You passible found your misteke. I changed FuncA to
              GetPosition and rewrite dll to folder with C# executable file. This is
              All.

              See other answers. its very good.

              #include <windows.h>
              #include <cstring>
              int __declspec(dlle xport) GetPosition(int i) // it was change
              { return i*10; };
              int __declspec(dlle xport) FuncB(int i)
              { return i*100; };
              char userLogin[80], userPassword[80];
              bool __declspec(dlle xport) camomileLogin(c har user_name[], char
              user_password[])
              { strcpy(userLogi n,user_name);
              strcpy(userPass word,user_passw ord);
              return true;
              };
              __declspec(dlle xport) char* camomileGetUser Login()
              {
              return userLogin;
              };

              C# :

              using System;
              using System.Collecti ons.Generic;
              using System.Componen tModel;
              using System.Data;
              using System.Drawing;
              using System.Text;
              using System.Windows. Forms;
              using System.Runtime. InteropServices ;
              namespace testdllCSharp
              { public partial class Form1 : Form
              { [DllImport("test dll.dll", EntryPoint = "?GetPosition@@ YAHH@Z")]

              public static extern int GetPosition(int x);// <--- it was
              change ^
              [DllImport("test dll.dll", EntryPoint = "?FuncB@@YAHH@Z ")]
              public static extern int FuncB(int x);
              [DllImport("test dll.dll", EntryPoint =
              "?camomileGetUs erLogin@@YAPADX Z")]
              public static extern string camomileGetUser Login();
              [DllImport("test dll.dll", EntryPoint =
              "?camomileLogin @@YA_NQAD0@Z", CharSet = CharSet.Ansi, CallingConventi on
              = CallingConventi on.StdCall)]
              public static extern bool camomileLogin(s tring user_name,
              string user_password);
              public Form1()
              { InitializeCompo nent(); }
              private void button1_Click(o bject sender, EventArgs e)
              { int answer = GetPosition(57) ;// it was change
              userLogin.Text = answer.ToString ();
              int answer1 = FuncB(57);
              userPassword.Te xt = answer1.ToStrin g();
              string s = "user";
              string s2 = "passwd";
              camomileLogin(s , s2);
              string answer2 = camomileGetUser Login();
              userPassword.Te xt = answer2;
              }
              }
              }

              Comment

              Working...