We help IT Professionals succeed at work.

error with external dll

vicomin
vicomin asked
on
I'm using an external dll and get the following error:

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

System.AccessViolationException was unhandled
  Message="Attempted to read or write protected memory. This is often an indication that other memory is corrupt."
  Source="OptoTest"
  StackTrace:
       at OptoTest.Form1.OP930_GetTemperature(Double tempC, Int32 units)
       at OptoTest.Form1.btnTempGet_Click(Object sender, EventArgs e) in C:\Documents and Settings\user\My Documents\Visual Studio 2008\Projects\OptoTest\OptoTest\Form1.cs:line 102
       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.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
       at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
       at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
       at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
       at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
       at System.Windows.Forms.Application.Run(Form mainForm)
       at OptoTest.Program.Main() in C:\Documents and Settings\user\My Documents\Visual Studio 2008\Projects\OptoTest\OptoTest\Program.cs:line 18
       at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
       at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
  InnerException:


using System.Runtime.InteropServices;

[DllImport("OP930M.DLL")]
        private static extern int OP930_GetTemperature(double tempC, int units);

        private void btnTempGet_Click(object sender, EventArgs e)
        {
            double temp = 0.0;
            status = OP930_GetTemperature(temp, 1);//errors here
            Label1.Text = Convert.ToString(temp);
        }

Open in new window

Comment
Watch Question

Commented:
It looks like the function you ar calling is taking a temperature, converting it and then passing it back i the same argument. This would indicate that the first argument is actually a pointer to a double instead of a double value. Try to modify your DLLImport statement to be as follows (note the added ref before the double):

[DllImport("OP930M.DLL")]
        private static extern int OP930_GetTemperature(ref double tempC, int units);

Commented:
I forgot to add that you will also need to modify your call to be:

status = OP930_GetTemperature(ref temp, 1);