Get the App
SLTechnology News&Howtos  ›  Development  › 

What is the method of C # calling Dll to pass string pointer parameters

Shulou Source: shulou.com Published: 2022-06-02 02:32:22 10月01日 Update

The main content of this article is to explain "what is the method of C # calling Dll to pass string pointer parameters", interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn what is the method of passing string pointer parameters by calling Dll in C #.

Recently, to write a small software with c #, to call dll written by VC, pass parameters to dll, because it is unmanaged code, so it is a bit troublesome to call, to convert the parameters into pointer parameters of unmanaged code, and then pass them to dllview plaincopy to clipboardprint?

Using System.Runtime.InteropServices; / / manipulate Dll's class space class CGicomIndex {[DllImport ("Index_dll.dll")] unsafe private static extern Int32 create_index_file (IntPtr filename, IntPtr fieldname, UInt32 tongshu, IntPtr err); / / VC prototype create_index_file (char*filename, char* fieldname, unsigned long tongshu, char* err) # region "bool CreateIndexFile (string m_strFileName, string m_strFieldName, UInt32 m_key Ref string strErr) create an index on the DBF file according to the specified field "/ A pair of DBF files create an index according to the specified field / DBF file path / the field name / the number of index buckets that create the index (the maximum number of file records ) / / error message / / successful true public static bool CreateIndexFile (string m_strFileName String m_strFieldName, UInt32 m_key, ref string strErr) {try {IntPtr ptrFileName, ptrFieldName, ptrErr / / apply for unmanaged space based on the length of the data ptrFileName = mallocIntptr (m_strFileName); ptrFieldName = mallocIntptr (m_strFieldName); ptrErr = mallocIntptr / / create an index if (create_index_file (ptrFileName, ptrFieldName, m_key, ptrErr)! = 1) {strErr = Marshal.PtrToStringAnsi (ptrErr); Marshal.FreeHGlobal (ptrFileName); Marshal.FreeHGlobal (ptrFieldName); Marshal.FreeHGlobal (ptrErr) Return false;} Marshal.FreeHGlobal (ptrFileName); Marshal.FreeHGlobal (ptrFieldName); Marshal.FreeHGlobal (ptrErr); return true;} catch (System.Exception ex) {strErr = ex.Message Return false } # endregion # region "IntPtr mallocIntptr (string strData) request unmanaged space according to the length of the data" / apply for unmanaged space according to the length of the data / data to apply for unmanaged space / / the pointer to the non-drag space private static IntPtr mallocIntptr (string strData) {/ / first converts the string to byte mode Byte [] btData = System.Text.Encoding.Default.GetBytes (strData) / / apply for non-towed space IntPtr m_ptr = Marshal.AllocHGlobal (btData.Length); / / clear 0 Byte [] btZero = new Byte [btData.Length + 1] for non-towed space; / / be sure to add 1, otherwise it will be garbled, and Marshal.Copy (btZero, 0, m_ptr, btZero.Length) cannot be found. / / assign Marshal.Copy (btData, 0, m_ptr, btData.Length) to the space pointed by the pointer; return m_ptr } / apply for unmanaged space based on length / to apply for the size of unmanaged space / pointer to unmanaged space private static IntPtr mallocIntptr (int length) {/ / apply for undragged space IntPtr m_ptr = Marshal.AllocHGlobal (length) / / clear 0 Byte [] btZero = new Byte [length + 1] to the non-drag space; / / be sure to add 1, otherwise it will be garbled, because Marshal.Copy (btZero, 0, m_ptr, btZero.Length) cannot be found; / / assign Marshal.Copy (btZero, 0, m_ptr, length) to the space pointed by the pointer Return masked ptrr;} # endregion} so far, I believe you have a deeper understanding of "what is the method of calling Dll to pass string pointer parameters". You might as well do it in practice! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Tags: Space pointer managed parameter index character string method point data file length Category field garbled code content reason learning utility Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MySQL Xiaomi Docker Huawei NVidia