473,322 Members | 1,719 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

Attempted to read or write protected memory while importing function from c++ dll

2
hi,
I have a native c++ dll and i am trying to import a function of the class in the dll to use it in c# so i wrote a wrapper :
Expand|Select|Wrap|Line Numbers
  1. public struct myclassUnman{
  2.  
  3.     public:
  4.         [DllImport("mydll.dll", 
  5.                   EntryPoint="??0myclass@@QAE@XZ", 
  6.                   CallingConvention=CallingConvention::ThisCall)]
  7.         static void ctor(myclassUnman*);
  8.  
  9.         [DllImport("mydll.dll", 
  10.                   EntryPoint="??1myclass@@QAE@XZ", 
  11.                   CallingConvention=CallingConvention::ThisCall)]
  12.         static void dtor(myclassUnman*);    
  13.  
  14.         [DllImport("mydll.dll", 
  15.                   EntryPoint="?getboolvalue@myclass@@QAE_NPAEH@Z", 
  16.                   CallingConvention=CallingConvention::ThisCall)]
  17.         static bool getboolvalue(myclassUnman*,unsigned char*,int);
  18.  
  19.     };
  20.     public ref class myclassWrap
  21.     {
  22.     public: 
  23.         myclassWrap(){
  24.             tv = new myclassUnman();
  25.             myclassUnman::ctor(mu);
  26.         }
  27.         ~myclassWrap(){
  28.             myclassUnman::dtor(mu);
  29.         }
  30.         bool getboolvalue(unsigned char* a,int b){
  31.             return myclassUnman::getboolvalue(mu,a,b);
  32.         }
  33.  
  34.     private:
  35.         myclassUnman *mu;
  36.     };    
  37.  
the prototype of function in the dll :
Expand|Select|Wrap|Line Numbers
  1. public: bool __thiscall myclass::getboolvalue(unsigned char *,int)
and finally in my c# code i call the wrapper but it gives an exception in the following line:
Expand|Select|Wrap|Line Numbers
  1. return myclassUnman::getboolvalue(tv,a,b);
Exception is:
Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
my c# code:
Expand|Select|Wrap|Line Numbers
  1.  unsafe
  2.             {
  3.                 myclassWrap mw = new myclassWrap();
  4.                 Console.WriteLine(mw.getboolvalue((byte*)11, 2));
  5.  
  6.              }         
  7.  
please help me.
Feb 15 '10 #1
0 1202

Sign in to post your reply or Sign up for a free account.

Similar topics

9
by: hiralparikh | last post by:
Hi, I am using .NET 2.0 and trying to use a function from a native DLL file. Here is the syntax that I am using: definition: public static extern String getPwd(String strServerName, String...
4
by: Seok Bee | last post by:
Dear Experts, I have created a script to extract the Event Logs from the system into an excel sheet. The logs are separated into 2 worksheets (Application Log and System Log). After this excel...
3
by: XJ | last post by:
Hi experts, i try to use vb.net 2005 call dll, then give me "Attempted to read or write protected memory.This is often an indication that other memory is corrupt". i have chk some message others...
1
by: ianyian | last post by:
Hi experts, i try to use vb.net 2005 call C++ dll, then give me "Attempted to read or write protected memory.This is often an indication that other memory is corrupt". i have chk some message...
2
by: Ilkka | last post by:
I have created an C++ application with Windows Forms, ADO and SQL server 2005. Now I need to change something and started debugging the code. Then suddenly I receive an error. "An unhandled...
2
by: Pieter | last post by:
Hi, Since 10 days (the first time was the 10th of november) I have some weird exception happening in an application here: All (except 1 of the total of 5) users had this error now 1 of 2 times...
2
by: adypoly | last post by:
Hi guys... I am having a typical problem in using one of the native dll in C# I'll explain what am trying to do, I've a dll written in C language which i am trying to include in my C# project,...
6
by: Sugandh Jain | last post by:
Hi, I am getting the error message Attempted to read or write protected memory. This is often an indication that other memory is corrupt. It was not coming until yet, for around 2 months. Now,...
6
by: Scott Gravenhorst | last post by:
Windows XP SP3 My application is set to open a SaveFile dialog when an exit is requested. When I click the app's close button, the save dialog opens, but when I click to change the folder, the...
3
by: sriram347 | last post by:
Hi I am a newbie to ASP.NET. I developed a web page (project type is web application) and I keep getting this error. B]Error message : "System.AccessViolation Exception attempted to read or...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.