vicomin
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.AccessViolationExce ption 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_GetTe mperature( Double tempC, Int32 units)
at OptoTest.Form1.btnTempGet_ Click(Obje ct sender, EventArgs e) in C:\Documents and Settings\user\My Documents\Visual Studio 2008\Projects\OptoTest\Opt oTest\Form 1.cs:line 102
at System.Windows.Forms.Contr ol.OnClick (EventArgs e)
at System.Windows.Forms.Butto n.OnClick( EventArgs e)
at System.Windows.Forms.Butto n.OnMouseU p(MouseEve ntArgs mevent)
at System.Windows.Forms.Contr ol.WmMouse Up(Message & m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.Contr ol.WndProc (Message& m)
at System.Windows.Forms.Butto nBase.WndP roc(Messag e& m)
at System.Windows.Forms.Butto n.WndProc( Message& m)
at System.Windows.Forms.Contr ol.Control NativeWind ow.OnMessa ge(Message & m)
at System.Windows.Forms.Contr ol.Control NativeWind ow.WndProc (Message& m)
at System.Windows.Forms.Nativ eWindow.De buggableCa llback(Int Ptr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.Unsaf eNativeMet hods.Dispa tchMessage W(MSG& msg)
at System.Windows.Forms.Appli cation.Com ponentMana ger.System .Windows.F orms.Unsaf eNativeMet hods.IMsoC omponentMa nager.FPus hMessageLo op(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
at System.Windows.Forms.Appli cation.Thr eadContext .RunMessag eLoopInner (Int32 reason, ApplicationContext context)
at System.Windows.Forms.Appli cation.Thr eadContext .RunMessag eLoop(Int3 2 reason, ApplicationContext context)
at System.Windows.Forms.Appli cation.Run (Form mainForm)
at OptoTest.Program.Main() in C:\Documents and Settings\user\My Documents\Visual Studio 2008\Projects\OptoTest\Opt oTest\Prog ram.cs:lin e 18
at System.AppDomain._nExecute Assembly(A ssembly assembly, String[] args)
at System.AppDomain.ExecuteAs sembly(Str ing assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.Hos tingProces s.HostProc .RunUsersA ssembly()
at System.Threading.ThreadHel per.Thread Start_Cont ext(Object state)
at System.Threading.Execution Context.Ru n(Executio nContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHel per.Thread Start()
InnerException:
Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
System.AccessViolationExce
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_GetTe
at OptoTest.Form1.btnTempGet_
at System.Windows.Forms.Contr
at System.Windows.Forms.Butto
at System.Windows.Forms.Butto
at System.Windows.Forms.Contr
at System.Windows.Forms.Contr
at System.Windows.Forms.Butto
at System.Windows.Forms.Butto
at System.Windows.Forms.Contr
at System.Windows.Forms.Contr
at System.Windows.Forms.Nativ
at System.Windows.Forms.Unsaf
at System.Windows.Forms.Appli
at System.Windows.Forms.Appli
at System.Windows.Forms.Appli
at System.Windows.Forms.Appli
at OptoTest.Program.Main() in C:\Documents and Settings\user\My Documents\Visual Studio 2008\Projects\OptoTest\Opt
at System.AppDomain._nExecute
at System.AppDomain.ExecuteAs
at Microsoft.VisualStudio.Hos
at System.Threading.ThreadHel
at System.Threading.Execution
at System.Threading.ThreadHel
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);
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
I forgot to add that you will also need to modify your call to be:
status = OP930_GetTemperature(ref temp, 1);
status = OP930_GetTemperature(ref temp, 1);
http://social.msdn.microsoft.com/forums/en-US/csharpgeneral/thread/6adca20b-649f-41a4-8fa1-09534882d76c
Check
http://forums.asp.net/t/675515.aspx