Link to home
Start Free TrialLog in
Avatar of vicomin
vicominFlag for United States of America

asked on

error with external dll

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

Avatar of Amandeep Singh Bhullar
Amandeep Singh Bhullar
Flag of India image

ASKER CERTIFIED SOLUTION
Avatar of sch13824
sch13824

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of sch13824
sch13824

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

status = OP930_GetTemperature(ref temp, 1);