By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,670 Members | 1,558 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,670 IT Pros & Developers. It's quick & easy.

PInvoke: where is SecureZeroMemory?

P: n/a
SecureZeroMemory is an alias of RtlSecureZeroMemory, an inline
function defined in WinBase.h.

I opened WinBase.h with a text editor but I didn't find any inline
definition; I only found:
#define SecureZeroMemory RtlSecureZeroMemory

However in C# I want to call SecureZeroMemory to zero a memory area
containing a password, but using user32.dll, kernel32.dll or
advapi32.dll with DllImport setting SecureZeroMemory or
RtlSecureZeroMemory as the entry point an exception says unable to
find entrypoint X in DLL Y.

[DllImport("kernel32.dll", CharSet = CharSet.Auto)]
private static extern void SecureZeroMemory(IntPtr ptr, IntPtr cnt);

Can you help me?

Thanks, Luigi.
Jul 10 '08 #1
Share this Question
Share on Google+
9 Replies


P: n/a
Luigi,

Well, SecureZeroMemory is nothing more than an alias that is resolved at
compile time in C++, it's not actually exported from the dll.

If you want to alias it as SecureZeroMemory, you can set the EntryPoint
property on the DllImport attribute to "RtlSecureZeroMemory":

[DllImport("kernel32.dll", CharSet = CharSet.Auto, EntryPoint =
"RtlSecureZeroMemory")]
private static extern void SecureZeroMemory(IntPtr ptr, IntPtr cnt);
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Luigi" <ne**********@gmail.comwrote in message
news:59**********************************@j22g2000 hsf.googlegroups.com...
SecureZeroMemory is an alias of RtlSecureZeroMemory, an inline
function defined in WinBase.h.

I opened WinBase.h with a text editor but I didn't find any inline
definition; I only found:
#define SecureZeroMemory RtlSecureZeroMemory

However in C# I want to call SecureZeroMemory to zero a memory area
containing a password, but using user32.dll, kernel32.dll or
advapi32.dll with DllImport setting SecureZeroMemory or
RtlSecureZeroMemory as the entry point an exception says unable to
find entrypoint X in DLL Y.

[DllImport("kernel32.dll", CharSet = CharSet.Auto)]
private static extern void SecureZeroMemory(IntPtr ptr, IntPtr cnt);

Can you help me?

Thanks, Luigi.

Jul 10 '08 #2

P: n/a
[DllImport("kernel32.dll", CharSet = CharSet.Auto, EntryPoint = "RtlSecureZeroMemory")]
private static extern void SecureZeroMemory(IntPtr ptr, IntPtr cnt);
Nicholas, I'm running VS 2005 SP1on a Windows XP SP3 machine and using
the code above I get an EntryPointNotFoundException :(
Jul 10 '08 #3

P: n/a
Luigi,

Ok, stupid me, this isn't actually exported as a function from a DLL.
Rather, you will have to find where it is inlined, and translate it for C#.

More than likely, the inlined code for RtlSecureZeroMemory has assembler
in it, which you won't be able to directly translate to C#. You might have
to create a dll in C++ which exposes the function and then make the call
from C#.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Luigi" <ne**********@gmail.comwrote in message
news:23**********************************@y21g2000 hsf.googlegroups.com...
>[DllImport("kernel32.dll", CharSet = CharSet.Auto, EntryPoint =
"RtlSecureZeroMemory")]
private static extern void SecureZeroMemory(IntPtr ptr, IntPtr cnt);
Nicholas, I'm running VS 2005 SP1on a Windows XP SP3 machine and using
the code above I get an EntryPointNotFoundException :(

Jul 10 '08 #4

P: n/a

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.comha
scritto nel messaggio news:u4**************@TK2MSFTNGP02.phx.gbl...
Luigi,

Well, SecureZeroMemory is nothing more than an alias that is resolved
at compile time in C++, it's not actually exported from the dll.

If you want to alias it as SecureZeroMemory, you can set the EntryPoint
property on the DllImport attribute to "RtlSecureZeroMemory":

[DllImport("kernel32.dll", CharSet = CharSet.Auto, EntryPoint =
"RtlSecureZeroMemory")]
private static extern void SecureZeroMemory(IntPtr ptr, IntPtr cnt);
It seems that there is no RtlSecureZeroMemory exported from kernel32.dll.

Try a dumpbin /EXPORTS kernel32.dll to check that.

RtlSecureZeroMemory is just an *inline* C function (defined in winnt.h in my
VS2008 installation).

Giovanni

Jul 10 '08 #5

P: n/a

"Luigi" <ne**********@gmail.comwrote in message
news:23**********************************@y21g2000 hsf.googlegroups.com...
>[DllImport("kernel32.dll", CharSet = CharSet.Auto, EntryPoint =
"RtlSecureZeroMemory")]
private static extern void SecureZeroMemory(IntPtr ptr, IntPtr cnt);
Nicholas, I'm running VS 2005 SP1on a Windows XP SP3 machine and using
the code above I get an EntryPointNotFoundException :(
The function is inlined so you can't call it this way. Write your own C++
wrapper class to call it and invoke this (wrapper) function from your C#
code. It's trivial if you already know VC++, otherwise there's a learning
curve.
Jul 10 '08 #6

P: n/a
From VS 2003 to VS 2008 SecureZeroMemory is declared in winbase.h as
an alias of RtlSecureZeroMemory that is defined as an inline function
in winnt.h.

I created a C++ solution containing a Win32 empty project with DLL
output and I included in it only this source (cpp) file:

#include <windows.h>

__declspec(dllexport) PVOID SecureZeroMem(IN PVOID ptr, IN SIZE_T cnt)
{
// PVOID SecureZeroMemory(IN PVOID ptr, IN SIZE_T cnt) is declared
in winBase.h as an alias for
// PVOID RtlSecureZeroMemory(IN PVOID ptr, IN SIZE_T cnt) defined
in winnt.h as an inline function:

return SecureZeroMemory(ptr, cnt);
}

After building the solution I moved the generated DLL into the Bin
\Debug folder of the C# project that PInvokes it and I added this C#
code:

[DllImport("SecureZeroMem.dll", EntryPoint = "SecureZeroMem", CharSet
= CharSet.Auto)]
private static extern void SecureZeroMemory(IntPtr ptr, IntPtr cnt);

Debugging my C# project I get an EntryPointNotFoundException :(

Could you tell me what is wrong?

Thanks, Luigi.
Jul 11 '08 #7

P: n/a
Could you tell me what is wrong?

Try:

extern "C" __declspec(dllexport) PVOID SecureZeroMem(IN PVOID ptr, IN SIZE_T
cnt)
{
// ...
}

Also, why do you need the "EntryPoint " clause. Just call it using the
original name.
Jul 11 '08 #8

P: n/a
Luigi wrote:
From VS 2003 to VS 2008 SecureZeroMemory is declared in winbase.h as
an alias of RtlSecureZeroMemory that is defined as an inline function
in winnt.h.

I created a C++ solution containing a Win32 empty project with DLL
output and I included in it only this source (cpp) file:

#include <windows.h>

__declspec(dllexport) PVOID SecureZeroMem(IN PVOID ptr, IN SIZE_T cnt)
{
// PVOID SecureZeroMemory(IN PVOID ptr, IN SIZE_T cnt) is declared
in winBase.h as an alias for
// PVOID RtlSecureZeroMemory(IN PVOID ptr, IN SIZE_T cnt) defined
in winnt.h as an inline function:

return SecureZeroMemory(ptr, cnt);
}

After building the solution I moved the generated DLL into the Bin
\Debug folder of the C# project that PInvokes it and I added this C#
code:

[DllImport("SecureZeroMem.dll", EntryPoint = "SecureZeroMem", CharSet
= CharSet.Auto)]
private static extern void SecureZeroMemory(IntPtr ptr, IntPtr cnt);

Debugging my C# project I get an EntryPointNotFoundException :(

Could you tell me what is wrong?
Should be declared like this:

extern "C" __declspec(dllexport) PVOID __stdcall SecureZeroMem(IN PVOID ptr,
IN SIZE_T cnt);

Then

PVOID __stdcall SecureZeroMem(...) {
...
}

Without __stdcall you'll get the wrong calling convention (you can import
cdecl functions, but stdcall is conventional in DLLs), without "extern "C""
the function name will be mangled (hence the "entry point not found").

The signature of your C# import should be

[DllImport("SecureZeroMem.dll", EntryPoint = "SecureZeroMem")]
private static extern IntPtr SecureZeroMemory(IntPtr ptr, IntPtr cnt);

(Note the return type.)

You can also use a .DEF file in the C++ project to remove the need for the
entry point remapping.

--
J.
Jul 11 '08 #9

P: n/a
// SecureZeroMem.cpp

#include <windows.h>

extern "C" __declspec(dllexport) PVOID __stdcall SecureZeroMem(IN
PVOID ptr, IN SIZE_T cnt)
{
return SecureZeroMemory(ptr, cnt);
}
// Caller.cs

[DllImport("SecureZeroMem.dll", CharSet = CharSet.Auto, SetLastError =
true)]
private static extern IntPtr SecureZeroMem(IntPtr ptr, uint cnt);
I solved all my problems with the above code (IntPtr cnt generates an
unhandled exception).
Thank you all!

Luigi.
Jul 11 '08 #10

This discussion thread is closed

Replies have been disabled for this discussion.