Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

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

2025-04-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report