472,126 Members | 1,591 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,126 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 21145
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

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.