C # to call C ++ dynamic library (dll)

In the actual software development process, because the company uses multiple languages ​​developed in C # may want to implement a feature, and the function may be in another language has been achieved, then we can call other modules language written it? There is, due to the good development of C # projects, we can use the reflector and other decompiler decompile the source code, so for some of the core algorithm, we do not want others to know, so in order to enhance the security of the code, we need to some of the core algorithm in C or C ++ to write, and then use C # to call these interfaces have been written. In the face of the above, how do we do it?


 Option One: re-implemented
        for the first case, we can be C or C ++ functions using C # to re-implement, so the code is relatively unified, maintenance more convenient, but then increases the cost of software development, the C ++ code function change involving pointer to C # and memory operation is relatively complicated, and since the development of good modules Why not reuse it? For the second case it can not be effectively addressed, although you can use an obfuscator obfuscate the code, but any course is not very safe.

 Option Two: package COM components
        we can function in C or C ++ is packaged as COM components, more convenient when you call in C #, but COM components need to be registered and multiple registrations may also cause some problems, but in dealing with C or C ++ COM components and type of type of conversion, it also may be some trouble.


 Option Three: the use of dynamic link library
        that we can directly call C or C ++ DLL has been written, this is more convenient, so a good solution to the above problems.

 

        In a real project, we need to use C # to call a number of interfaces in C ++, so I use a program three dynamic library, here I was in practice, explained how to deal with.
        In the process of calling the dynamic library I have encountered some of the following questions:
        1, C ++ has pointers, C # using pointers you need?
        Because C ++ dynamic library has a pointer parameter, so I also use unsafe code in .NET, C # using pointers, but eventually there were some problems, such as incoming parameters in C # is a two-dimensional array when there is a problem, the problem I find a lot of information on the internet have not been solved finally and c ++ programmers to discuss the next change in the parameter type arguments passed. Finally, do not use pointers.
        2, how C # and C ++ casts it?
        Although C # and C ++ relatively similar, but it gives us the parameters we have to one correspondence with the type parameters of type C # up, so I got some information on the correspondence of its type, specifically to see the follow-up instructions.
        3, C ++ DLL written into that position it?
        About the position of C ++ dynamic library is also a problem in the application you used a relative and absolute paths for testing, some found in VS can be called that, but after the release of discovery can not be invoked to the dynamic library last as long as the dynamic dll system32 directory into the following system before we can solve the problem of change, yet to find other ways, if any other better method also please guidance.
        4, the name of the dll how to decompile C ++, the port?
        Decompile can view information about the dynamic library written by someone else by Dependency Walker tool
        5, there are other details, such as C # dynamic library calls need to specify the encoding code writing and so on

c # c ++ dynamic library calls in general we write

[DllImport("UCamer.dll", CallingConvention = CallingConvention.Winapi)]
public extern static void Disp_Destroy(IntPtr hShow);
The first parameter is the path UCamer.dll DllImport the dynamic library dll, running on this dll directory or c: windows / sytem32 under

  CallingConvention argument is to call c # c ++ approach is explained in the following enumeration msdn

  

Cdecl The caller clean up the stack. This enables you to call having  varargs  function (e.g., in  the Printf ), so as to accept a variable number of parameters of the method can be used. 
FastCall It does not support this calling convention.
StdCall Callee clean up the stack. This is done using the platform invoke call unmanaged functions by default convention. 
ThisCall The first parameter is  this  pointer which is stored in the ECX register. Other parameters are pushed onto the stack. This calling convention for calling the class method exported from an unmanaged DLL. 
Winapi This member is not actually calling convention, but the use of the default platform calling convention. For example, on Windows defaults to  StdCall , CE.NET default on Windows  Cdecl

 Winapi mode is selected automatically calling convention is viewed from above, according to the system. The thisCall is a call to the c ++ classes. So under normal circumstances we choose Winapi it.

example:

Copy the code
        Untitled Form #region right of the taskbar pop-up menu code 

        [DllImport ( "user32.dll", EntryPoint = "the GetWindowLong", the CharSet = CharSet.Auto)] 
        public static extern int the GetWindowLong (HandleRef hWnd, int nIndex); 

        [DllImport ( "User32.dll", the EntryPoint = "the SetWindowLong", the CharSet = CharSet.Auto)] 
        public static extern IntPtr the SetWindowLong (HandleRef the hWnd, int nIndex, int dwNewLong); 

        protected the override the CreateParams the CreateParams 
        { 
            GET 
            { 
                const int WS_MINIMIZEBOX = 0x00020000; // Winuser.h defined in    
                the CreateParams CP = base.CreateParams;  
                cp.Style = cp.Style | WS_MINIMIZEBOX; // allows to minimize the operation   
                return CP; 
            } 
        }

        #endregion

        #region 窗体拖动代码
        [DllImport("user32.dll")]
        public static extern bool ReleaseCapture();
        [DllImport("user32.dll")]
        public static extern bool SendMessage(IntPtr hwnd, int wMsg, int wParam, int lParam);
        public const int WM_SYSCOMMAND = 0x0112;
        public const int SC_MOVE = 0xF010;
        public const int HTCAPTION = 0x0002;
        private void Login_MouseDown(object sender, MouseEventArgs e)
        {
            ReleaseCapture();
            SendMessage(this.Handle, WM_SYSCOMMAND, SC_MOVE + HTCAPTION, 0);
        }
        #endregion


        [DllImport("wininet.dll")]
        private extern static bool InternetGetConnectedState(out int conn, int val);
        private void btnNetTest_Click(object sender, EventArgs e)
        {
            int Out;
            if (InternetGetConnectedState(out Out, 0) == true)
            {
                MessageDxUtil.ShowTips("Internet网络连通!");
            }
            else
            {
                MessageDxUtil.ShowTips("Internet网络不通!");
            }

        }
Copy the code

 

Life inconvenient, with software solutions, learning instead, a white school ----- red wagon

Guess you like

Origin www.cnblogs.com/wwwbdabc/p/11653243.html