C\VC++ and VC++ call dll detailed process, export the corresponding relationship of data types of imported functions, and return the string implementation method

Using vc++ to develop drawing components based on MFC dynamic link library (dll), how to use VC dll in c\? What is the corresponding relationship between the data type of c\; import function and the data type of dll export? C\c how to get the string of dll function? How to use dll in VC project?

1, VC DLL interface definition

To develop the cloud drawing component fydc DLL, fydc DLL is a VC MFC dynamic link library, which is composed of multiple class libraries, including MFC classes, forms, icons and other resources. The coding definition and process are as follows:

1. create the MFC general dynamic link library dll project FYDC;

2. implement CSLine line drawing, CSEllipse circle drawing and other classes, and realize all drawing functions such as canvas CCanvasWnd;

3. in fydc H and fydc CPP add a drawing class library header file, such as canvaswnd h, SLine. H, etc;

4. in fydc CPP defines and implements the export interface (some interfaces are as follows).

1> extern "C" __declspec(dllexport) CCanvas* New()
2> extern "C" __declspec(dllexport) void Create(CCanvas* canvas, HWND hParentWnd, int x, int y, int w, int h)
3> extern "C" __declspec(dllexport) void Load(CCanvas* canvas, const char* fileName)

4> extern "C" __declspec(dllexport) int GetCount(CCanvas* canvas)
5> extern "C" __declspec(dllexport) CSLine* GetAt(CCanvas* canvas, int index)
6> extern "C" __declspec(dllexport) void GetName(CSLine* line, wchar_t* name)

7> extern "C" __declspec(dllexport) void GetSize(CCanvas* canvas, int& width, int& height)

5. compile and generate fydc dll and fydc Lib, that is to complete dll development for reuse by third-party projects.

 

2, C\

1. create a new C\c project and copy fydc DLL to the generated exe directory;

2. add fydc DLL import function declaration. The C\n\e import functions corresponding to the above 7 export functions are declared as follows:

1> [DllImport("FYDC.dll", EntryPoint = "New", CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr New();
2> [DllImport("FYDC.dll", EntryPoint = "Create", CallingConvention = CallingConvention.Cdecl)]
public static extern void Create(IntPtr canvas, IntPtr hParentWnd, int x, int y, int w, int h);
3> [DllImport("FYDC.dll", EntryPoint = "Load", CallingConvention = CallingConvention.Cdecl)]
public static extern void Load(IntPtr canvas, string fileName);

4> [DllImport("FYDC.dll", EntryPoint = "GetCount", CallingConvention = CallingConvention.Cdecl)]
public static extern int GetCount(IntPtr canvas);
5> [DllImport("FYDC.dll", EntryPoint = "GetAt", CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr GetAt(IntPtr canvas, int index);
6> [DllImport("FYDC.dll", EntryPoint = "GetName", CallingConvention = CallingConvention.Cdecl)]
public static extern void GetName(IntPtr line, ref byte title);

7> [DllImport("FYDC.dll", EntryPoint = "GetSize", CallingConvention = CallingConvention.Cdecl)]
public static extern void GetSize(IntPtr canvas, ref int width, ref int height);

DLL export function type corresponds to C\; import function type

Function return value CCanvas* function return value IntPtr

Function parameter CSLine* function parameter IntPtr

Function parameter HWND function parameter IntPtr

Function return value or parameter int function return value or parameter int

Function parameter &int function parameter ref int

Function parameter const char* function parameter string

Function parameter char* function parameter ref byte

 

Note: when the function dll formal parameter sends out a string, the string needs to be copied in depth before it can be correctly returned to C#

The DLL return string function is implemented as follows:

extern "C" __declspec(dllexport) void GetName(CSLine* line, char* name)
{
	if (shape != line)
	{
        CString s = line->GetName();

		char attr[200];
		memset(attr, 0, sizeof(attr));
		memcpy(attr, s.GetBuffer(), s.GetLength());
		memcpy(name, attr, sizeof(attr));
	}
}

Examples of the correct use of this interface in the C\

byte[] s = new byte[200];
lock (s)
{
    GetName(shape, ref s[0]);  
}
string sName = System.Text.Encoding.Default.GetString(s, 0, s.Length);
sName = sName.Substring(0, sName.IndexOf('\0'));

When C\n\e reads the dll Chinese, it is because the coding agreement between the two parties is not unified. In this case, you should:

① dll project set to multibyte encoding

② dll interface implementation from char* to wchar_t*, and call the MultiByteToWideChar function to transcode

③ The formal parameters of the C\\C import function remain unchanged, only encoding Change default to encoding Unicode

 

3. you can directly refer to the export function here in other C\.  

 

3, Detailed steps for VC to use dll

1. create a new VC++ project and copy fydc DLL to the generated exe directory and copy fydc Lib to the project source code directory (not exe directory);

2. in the VC code file, add \pragma comment(lib,"FYDC.lib") to add the new type export declaration and function export declaration. The C\n\e import functions corresponding to the above 7 export functions are declared as follows:

#pragma comment(lib,"FYDC.lib")
extern "C" __declspec(dllimport) class CCanvas;
extern "C" __declspec(dllimport) class CSLine;

1> extern "C" __declspec(dllimport) CCanvas * New();
2> extern "C" __declspec(dllimport) void Create(CCanvas * canvas, HWND hParentWnd, int x, int y, int w, int h);
3> extern "C" __declspec(dllimport) void Load(CCanvas* canvas, const char* fileName);

4> extern "C" __declspec(dllimport) int GetCount(CCanvas* canvas);
5> extern "C" __declspec(dllimport) CSLine* GetAt(CCanvas* canvas, int index);
7> extern "C" __declspec(dllimport) void GetName(CSLine* line, char* name);

7> extern "C" __declspec(dllimport) void GetSize(CCanvas* canvas, int& width, int& height);

Because the dll is written in C++, the export function declaration referenced by the VC project is the same.

3. directly call the function of the declared dll in the VC code file.

Posted by Savahn on Wed, 01 Jun 2022 07:52:53 +0530