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

H

hiralparikh

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
 
N

Nicholas Paldino [.NET/C# MVP]

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.
 
H

hiralparikh

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
 
W

Willy Denoyette [MVP]

| 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.
 
H

hiralparikh

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
 
W

Willy Denoyette [MVP]

| 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.
 
H

hiralparikh

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
 
W

Willy Denoyette [MVP]

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 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
|
 
H

hiralparikh

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
 
H

hiralparikh

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top