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

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

string a = textBox1.Text;

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 **************
Assembly Version:
Win32 Version: 2.0.50727.3607 (GDR.050727-3600)
CodeBase: file:///c:/Windows/Microsoft.NET/Framework/v2.0.50727/mscorlib.dll
Assembly Version:
Win32 Version:
CodeBase: file:///D:/Debug/WindowsFormsApplication1.exe
Assembly Version:
Win32 Version: 2.0.50727.3053 (netfxsp.050727-3000)
CodeBase: file:///C:/Windows/assembly/GAC_MSIL/System.Windows.Forms/
Assembly Version:
Win32 Version: 2.0.50727.3614 (GDR.050727-3600)
CodeBase: file:///C:/Windows/assembly/GAC_MSIL/System/
Assembly Version:
Win32 Version: 2.0.50727.3053 (netfxsp.050727-3000)
CodeBase: file:///C:/Windows/assembly/GAC_MSIL/System.Drawing/

************** 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 section.
The application must also be compiled with debugging

For example:

< 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?

Updated 2-Feb-16 20:45pm
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...

1 solution

Share this answer
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...
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?
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)

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