473,692 Members | 2,411 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

native dll access error - Attempted to read or write protected memory

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:
[DllImport(@"old .dll", EntryPoint="#1" )]
public static extern String getPwd(String strServerName, String
strUserId);

Call:
string pwd = getPwd("servern ame", "myusername ");

I get the following error on the above line:
_______________ _
Attempted to read or write protected memory. This is often an
indication that other memory is corrupt.
Description: An unhandled exception occurred during the execution of
the current web request. Please review the stack trace for more
information about the error and where it originated in the code.

Exception Details: System.AccessVi olationExceptio n: Attempted to read
or write protected memory. This is often an indication that other
memory is corrupt.
_______________ ____

I have read the MSDN articles on these issues but those solutions are
not working.

Foll is the output of the 'link' command about the dll:

link -dump -exports old.dll
File Type: DLL

Section contains the following exports for old.dll

00000000 characteristics
3A352AEB time date stamp Mon Dec 11 14:28:43 2000
0.00 version
1 ordinal base
1 number of functions
1 number of names

ordinal hint RVA name

1 0 000012F0 _pwGet@12

Summary

5000 .data
2000 .rdata
1000 .reloc
A000 .text
Any help , direction is greatly appreciated.

Thanks,
Hiral

Jan 26 '06 #1
9 21334
This is because you are returning a String. If your function returns a
pointer to a character array, then you have to set the return value to an
IntPtr, and then marshal the string yourself.

You also have to make sure you de-allocate the memory allocated by the
function for the string (most likely through another interop call).

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

<hi*********@gm ail.com> wrote in message
news:11******** **************@ g43g2000cwa.goo glegroups.com.. .
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:
[DllImport(@"old .dll", EntryPoint="#1" )]
public static extern String getPwd(String strServerName, String
strUserId);

Call:
string pwd = getPwd("servern ame", "myusername ");

I get the following error on the above line:
_______________ _
Attempted to read or write protected memory. This is often an
indication that other memory is corrupt.
Description: An unhandled exception occurred during the execution of
the current web request. Please review the stack trace for more
information about the error and where it originated in the code.

Exception Details: System.AccessVi olationExceptio n: Attempted to read
or write protected memory. This is often an indication that other
memory is corrupt.
_______________ ____

I have read the MSDN articles on these issues but those solutions are
not working.

Foll is the output of the 'link' command about the dll:

link -dump -exports old.dll
File Type: DLL

Section contains the following exports for old.dll

00000000 characteristics
3A352AEB time date stamp Mon Dec 11 14:28:43 2000
0.00 version
1 ordinal base
1 number of functions
1 number of names

ordinal hint RVA name

1 0 000012F0 _pwGet@12

Summary

5000 .data
2000 .rdata
1000 .reloc
A000 .text
Any help , direction is greatly appreciated.

Thanks,
Hiral

Jan 26 '06 #2
Hi Nicholas,
I am not sure if the dll function returns a character array. Is there a
way I can check that?
Also, one other project uses a VB call to the dll function and it just
uses String.

Any ideas?
Is there any documentation that would point me to the right direction?
After googling around, it seems that this error 'Attempted to read or
write protected memory...' is occurring in .NET 2.0 in many scenarios.
Is it a bug in .net 2.0? I haven't tried running this function in .net
1.1.

Hiral

Jan 26 '06 #3

<hi*********@gm ail.com> wrote in message
news:11******** **************@ o13g2000cwo.goo glegroups.com.. .
| Hi Nicholas,
| I am not sure if the dll function returns a character array. Is there a
| way I can check that?
| Also, one other project uses a VB call to the dll function and it just
| uses String.
|
| Any ideas?
| Is there any documentation that would point me to the right direction?
| After googling around, it seems that this error 'Attempted to read or
| write protected memory...' is occurring in .NET 2.0 in many scenarios.
| Is it a bug in .net 2.0? I haven't tried running this function in .net
| 1.1.
|
| Hiral
|

Please post the VB declaration. My guess is that it returns a BSTR, in which
case you can do as Nick said, declare your function to return an IntPtr in
C# and marshal the IntPtr to a managed String using Marshal.PtrToSt ringBSTR.

Willy.
Jan 26 '06 #4
Hi,
Thanks for your messages, but I am still stuck.
I am moved a step further, though. I was getting the last error message
bcoz I was calling the native function incorrectly. It required a 3rd
parameter by reference that gets populated by the pwd.

But I don't get anything back from the native function. It seems that
the data type doesn't seem to be right because the function is not able
to lookup the server and username.
I am 100% sure that the native call works because the provided test EXE
works.

I have tried passing the foll formats:

int status = getPwd(chArrSer ver, chArrUser, ref chArrPwd);
int status = getPwd(strServe r, strUser, ref strPwd);
int status = getPwd(ptrServe r, ptrUser, ref ptrPwd);

Following is a c++ code that works. But I need to do this in my asp.net
2.0/c# app
int pwStatus;
char pwd[256];
char user[256];
char server[256];

pwStatus= getPwd(server, user, pwd);

Ideas???

Thanks,
Hiral

Jan 27 '06 #5

<hi*********@gm ail.com> wrote in message
news:11******** *************@o 13g2000cwo.goog legroups.com...
| Hi,
| Thanks for your messages, but I am still stuck.
| I am moved a step further, though. I was getting the last error message
| bcoz I was calling the native function incorrectly. It required a 3rd
| parameter by reference that gets populated by the pwd.
|
| But I don't get anything back from the native function. It seems that
| the data type doesn't seem to be right because the function is not able
| to lookup the server and username.
| I am 100% sure that the native call works because the provided test EXE
| works.
|
| I have tried passing the foll formats:
|
| int status = getPwd(chArrSer ver, chArrUser, ref chArrPwd);
| int status = getPwd(strServe r, strUser, ref strPwd);
| int status = getPwd(ptrServe r, ptrUser, ref ptrPwd);
|
| Following is a c++ code that works. But I need to do this in my asp.net
| 2.0/c# app
| int pwStatus;
| char pwd[256];
| char user[256];
| char server[256];
|
| pwStatus= getPwd(server, user, pwd);
|
| Ideas???
|
| Thanks,
| Hiral
|

Here is what I guess it should look like (not guaranteed as long as you
don't post the native declaration though):

[DllImport(@"old .dll", EntryPoint="#1" )]
public static extern int getPwd(String strServerName, String
strUserId, string StrPwd);
int status = getPwd(strServe r, strUser, strPwd);
Willy.
Jan 27 '06 #6
Hi Wily,
This is the exposed dll function call that I got by using dumpbin.exe.
Does this make any sense?

Dump of file c:\old.dll

File Type: DLL

Section contains the following exports for old.dll

00000000 characteristics
3A352AEB time date stamp Mon Dec 11 14:28:43 2000
0.00 version
1 ordinal base
1 number of functions
1 number of names

ordinal hint RVA name

1 0 000012F0 _getPwd@12

Summary

5000 .data
2000 .rdata
1000 .reloc
A000 .text
Thanks,
Hiral

Jan 30 '06 #7
No, it doesn't make sense, the dumpbin doesn't return the function
prototype, you need the documented declaration or an include file.
Did you try the declaration I gave you?

Willy.

<hi*********@gm ail.com> wrote in message
news:11******** **************@ f14g2000cwb.goo glegroups.com.. .
| Hi Wily,
| This is the exposed dll function call that I got by using dumpbin.exe.
| Does this make any sense?
|
| Dump of file c:\old.dll
|
| File Type: DLL
|
| Section contains the following exports for old.dll
|
| 00000000 characteristics
| 3A352AEB time date stamp Mon Dec 11 14:28:43 2000
| 0.00 version
| 1 ordinal base
| 1 number of functions
| 1 number of names
|
| ordinal hint RVA name
|
| 1 0 000012F0 _getPwd@12
|
| Summary
|
| 5000 .data
| 2000 .rdata
| 1000 .reloc
| A000 .text
|
|
| Thanks,
| Hiral
|
Jan 30 '06 #8
Yes,
I tried the declaration that you gave. But same result -- no error, but
no valid return value as well.

I think this is the decalaration from the c++ code:

#ifdef __cplusplus
extern "C" { // only need to export C interface if
// used by C++ source code
#endif

__declspec( dllimport ) int __stdcall getPwd(char *server, char *user,
char *pwGot);

#ifdef __cplusplus
}
#endif
Thanks Wily for your persistent help!

Hiral

Jan 30 '06 #9
Ok,
I finally found the problem... and the solution..
The 3rd parameter (pwd) was not to be passed by reference String.
I passed it just as StringBuilder and it worked.

remember, String is immutable!!!!

Thanks guys
Hiral

Jan 30 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
2065
by: Dhruba Bandopadhyay | last post by:
I have a problem. I have a C++ service and added a new API to it. I re-registered the service and re-added the reference in my ASP.NET Visual Web Developer. However when I call the new API I get: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
3
11936
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 people posted, but since still cannot, pls give me some commnet.(this dll run smoothly at vb 6 ) module: ' example Declare Function TwTsxInitPar Lib "TSDKVC.DLL" _
1
3166
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 others people posted, but since still cannot, pls give me some commnet.(this dll run smoothly at vb 6 ) module: ' example
0
1368
by: beluga | last post by:
Hi, I have an application written in C# which will use some custom libraries written in native C++. To bridge between the C# application layer and the native C++ library side, I created an IJW assembly using mixed mode C++/CLI and native C++. From the C# application side, I need to pass a window handle to the custom library so that inside this library, it will do some drawing on this screen. So, I from the C# application passed a...
2
21973
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 exception of type 'System.AccessViolationException' occurred in mscorlib.dll Additional information: Attempted to read or write protected memory. This is often an indication that other memory is corrupt." The progran ends on a windows form designer...
1
2415
by: Michael | last post by:
Hi Everyone, This is a first for me. I'm getting this error: Attempted to read or write protected memory. This is often an indication that other memory is corrupt. This happens when the dataAdapter tries to fill the dataset. This is the only place in my code that this happens. This is the only function that this happens. The code is below: Private Sub Button1_Click( ByVal sender As System.Object, ByVal e As System.EventArgs) Handles...
0
1266
by: bardiyam | last post by:
hi to all, can any body help me to solve this error.... unhandled exception : attempted to read or write protected memory. this is often indicates that other memory is corrupt + windows formsVC++2005.net im nt getting why this error is coming.. plz help... thanks in advance...
1
8078
by: Thomas Due | last post by:
Hi, I manage an rather old application in which we have some fairly complex (ugly) Delphi code. This is Delphi 6 we're talking about. Among all this Delphi code there is method for formating a print layout for slip printers. Pass a few parameters to this method and it returns a Delphi string which contains the entire slip, ready for sending to the slip printer. Now, we're slowly migrating to .NET and am in need of printing this slip...
6
4494
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 exception occurs pointing to FileSaveDialog1.ShowDialog(). The exception also indicates some problem with system.drawing.dll. The exception text is: "Attempted to read or write protected memory. This is often an
0
8610
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, weíll explore What is ONU, What Is Router, ONU & Routerís main usage, and What is the difference between ONU and Router. Letís take a closer look ! Part I. Meaning of...
0
8545
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8968
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8810
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8810
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
4327
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4562
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2978
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2242
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.