Click here to Skip to main content
15,884,628 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
My Delphi Procedure is:
Delphi
procedure seturunAdiGonder(_urunAdi: string); stdcall;
begin
  urunAdiGonder(Terazi, _urunAdi);
end;


And my C# Code:
C#
 [DllImport("UcgeBizerbaScale.dll",
                   CallingConvention = CallingConvention.StdCall,
                   CharSet = CharSet.Ansi)]
        public static extern void seturunAdiGonder(string ad);

string a = textBox1.Text;
            seturunAdiGonder(a);

when i use c# code i got an error "Unhandled exception has occured in your application.If you click Continue,the application ignore this error and attemot to continue.If you click quit,the application will close immediately

Attempted to read or write protected memory.This is often an indication that other memory is currupt.".

Detail Part:"
See the end of this message for details on invoking
just-in-time (JIT) debugging instead of this dialog box.

************** Exception Text **************
System.AccessViolationException: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
at WindowsFormsApplication1.Form1.seturunAdiGonder(String ad)
at WindowsFormsApplication1.Form1.button4_Click(Object sender, EventArgs e)
at System.Windows.Forms.Control.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ButtonBase.WndProc(Message& m)
at System.Windows.Forms.Button.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)


************** Loaded Assemblies **************
mscorlib
Assembly Version: 2.0.0.0
Win32 Version: 2.0.50727.3607 (GDR.050727-3600)
CodeBase: file:///c:/Windows/Microsoft.NET/Framework/v2.0.50727/mscorlib.dll
----------------------------------------
WindowsFormsApplication1
Assembly Version: 1.0.0.0
Win32 Version: 1.0.0.0
CodeBase: file:///D:/Debug/WindowsFormsApplication1.exe
----------------------------------------
System.Windows.Forms
Assembly Version: 2.0.0.0
Win32 Version: 2.0.50727.3053 (netfxsp.050727-3000)
CodeBase: file:///C:/Windows/assembly/GAC_MSIL/System.Windows.Forms/2.0.0.0__b77a5c561934e089/System.Windows.Forms.dll
----------------------------------------
System
Assembly Version: 2.0.0.0
Win32 Version: 2.0.50727.3614 (GDR.050727-3600)
CodeBase: file:///C:/Windows/assembly/GAC_MSIL/System/2.0.0.0__b77a5c561934e089/System.dll
----------------------------------------
System.Drawing
Assembly Version: 2.0.0.0
Win32 Version: 2.0.50727.3053 (netfxsp.050727-3000)
CodeBase: file:///C:/Windows/assembly/GAC_MSIL/System.Drawing/2.0.0.0__b03f5f7f11d50a3a/System.Drawing.dll
----------------------------------------

************** JIT Debugging **************
To enable just-in-time (JIT) debugging, the .config file for this
application or computer (machine.config) must have the
jitDebugging value set in the system.windows.forms section.
The application must also be compiled with debugging
enabled.

For example:

<configuration>
<system.windows.forms jitdebugging="true">


When JIT debugging is enabled, any unhandled exception
will be sent to the JIT debugger registered on the computer
rather than be handled by this dialog box.".

can anybody help me?

Thanks
Posted
Updated 2-Feb-16 20:45pm
v2
Comments
Sergey Alexandrovich Kryukov 3-Feb-16 3:41am    
Delphi has its own (very reasonable) layout for string. Can you modify your Delphi code to provide appropriate string type, such as pChar or pWideChar in some wrapper function to export? Otherwise you would need to write custom marshaling for this parameter...
—SA

1 solution

 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 3-Feb-16 3:45am    
And which of the answers do you consider relevant to this case? Perhaps it's using WideString marshaled as BStr, but maybe better solution would be changing Delphi type to pWideChar, then the default marshaling will work. For ANSI, pChar can be used. The problem with strcall is solved by the inquirer...
—SA
Member 10525430 3-Feb-16 6:34am    
Sergey thanks i fix the problem and CPallini thanks to you
Sergey Alexandrovich Kryukov 3-Feb-16 9:58am    
Sure. What types did you use for string?
—SA
Member 10525430 3-Feb-16 10:38am    
i used pWideChar

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900