Recently, I wrote a small software in C#. I want to call the dll written by VC and pass the parameters to the dll. Since it is unmanaged code, it is a bit troublesome to call it. I have to convert the parameters into pointer parameters of the unmanaged code and then pass it to the dll.view plaincopy to clipboardprint?
using ; //A class space for operating Dll 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 indexes for DBF files according to the specified fields" /// <summary> /// Create indexes for DBF files according to specified fields /// </summary> /// <param name="m_strFileName">DBF file path</param> /// <param name="m_strFieldName">Create indexed field name</param> /// <param name="m_key">Number of index buckets (maximum number of file records)</param> /// <param name="strErr">Error message</param> /// <returns>Successful true</returns> public static bool CreateIndexFile( string m_strFileName, string m_strFieldName, UInt32 m_key, ref string strErr ) { try { IntPtr ptrFileName, ptrFieldName, ptrErr; //Application for unmanaged space based on the length of the data ptrFileName = mallocIntptr(m_strFileName); ptrFieldName = mallocIntptr(m_strFieldName); ptrErr = mallocIntptr(300); //Create an index if (create_index_file(ptrFileName, ptrFieldName, m_key, ptrErr) != 1) { strErr = (ptrErr); (ptrFileName); (ptrFieldName); (ptrErr); return false; } (ptrFileName); (ptrFieldName); (ptrErr); return true; } catch ( ex) { strErr = ; return false; } } #endregion #region " IntPtr mallocIntptr( string strData ) applies for unmanaged space based on the length of the data" /// <summary> /// Apply for unmanaged space based on the length of the data /// </summary> /// <param name="strData">Data to apply for unmanaged space</param> /// <returns> Pointer to non-drag space</returns> private static IntPtr mallocIntptr( string strData ) { //Convert the string into bytes first Byte[] btData = (strData); //Apply for non-drag space IntPtr m_ptr = (); //Cleave unmanaged space 0 Byte[] btZero = new Byte[btData .Length+ 1]; //Be sure to add 1, otherwise there will be garbled code afterwards, and the reason is not found (btZero, 0, m_ptr, ); // Assign value to the space pointed to by the pointer (btData, 0, m_ptr, ); return m_ptr; } /// <summary> ///Application for unmanaged space based on length /// </summary> /// <param name="strData">The size of the unmanaged space to be applied for</param> /// <returns> Pointer to non-drag space</returns> private static IntPtr mallocIntptr( int length ) { //Apply for non-drag space IntPtr m_ptr = (length); //Cleave unmanaged space 0 Byte[] btZero = new Byte[length + 1]; //Be sure to add 1, otherwise there will be garbled code afterwards, and the reason is not found (btZero, 0, m_ptr, ); // Assign value to the space pointed to by the pointer (btZero, 0, m_ptr, length); return m_ptr; } #endregion }
This is the article about C# calling Dll to pass string pointer parameters. For more related C# string pointer parameters, please search for my previous article or continue browsing the related articles below. I hope everyone will support me in the future!