473,396 Members | 2,039 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,396 software developers and data experts.

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("servername", "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.AccessViolationException: 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 21296
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.com

<hi*********@gmail.com> wrote in message
news:11**********************@g43g2000cwa.googlegr oups.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("servername", "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.AccessViolationException: 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*********@gmail.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.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.PtrToStringBSTR.

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(chArrServer, chArrUser, ref chArrPwd);
int status = getPwd(strServer, strUser, ref strPwd);
int status = getPwd(ptrServer, 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*********@gmail.com> wrote in message
news:11*********************@o13g2000cwo.googlegro ups.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(chArrServer, chArrUser, ref chArrPwd);
| int status = getPwd(strServer, strUser, ref strPwd);
| int status = getPwd(ptrServer, 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(strServer, 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*********@gmail.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.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
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...
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...
0
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...
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...
1
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...
0
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...
1
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...
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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...
0
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...

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.