I have written a Banking System DLL in C++ with a C Interface with the following function that is used to rename a customer:
int renameCustomer(const string& firstname_, const string& name_, const unsigned int customerId_);
[DllImport("BankManagement.dll", EntryPoint = "renameCustomer", CallingConvention = CallingConvention.Cdecl)]
public static extern int renameCustomer(ref string firstname_, ref string name_, uint customerId_);
public int changeCustomerName(uint id_, string firstname_, string name_)
return renameCustomer(ref firstname_, ref name_, id_);
BankManageIntern.BankIntern myBankIntern = new BankManageIntern.BankIntern();
int checkCust = myBankIntern.changeCustomerName(0, "Name", "Firstname");
try the following:
instead of trying to pass an string, use an pointer to an char. this can be done by using an IntPtr.
private static extern int renameCustomer( IntPtr firstname, IntPtr lastname, uint id);
in your Application you can use the var datatype. It's similar to the auto_ptr in C++.
var firstnamePtr = Marshal.StringToHGlobalAnsi(firstname); var lastnamePtr = Marshal.StringToHGlobalAnsi(lastname);
and call with that your function from the DLL.
var status = renameCustomer(firstnamePtr, lastnamePtr, id);
Hope that will help you with your problem! :)