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

PInvoke: where is SecureZeroMemory?

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
9 9035
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
[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
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

"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

"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
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
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
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
// 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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Ted | last post by:
Is it possible to use mailslots in .NET using PInvoke? I have a VC++ 6.0 based app that creates and listens to a mailslot. I have a second VC++ 6.0 based app that opens the mailslot and writes...
5
by: Carlos Guzmán Álvarez | last post by:
Hello: I'm trying to execute a function of a unmanaged dll using PInvoke, i have definied the function as: public static extern int isc_dsql_prepare( int status_vector, ref int...
3
by: Brett Robichaud | last post by:
I have created a simple background thread to make one pinvoke call into a DLL I've created. My Winforms app spawns the thread in the form load event then go about it's business. The problem is...
5
by: vertigo | last post by:
Hello I use some win 32 API function for example: HANDLE CreateFile( LPCTSTR lpFileName, DWORD dwDesiredAccess, DWORD dwShareMode, LPSECURITY_ATTRIBUTES lpSecurityAttributes, DWORD...
5
by: _iycrd | last post by:
After numerous problems, I'm having second thoughts about using C++/CLI to wrap a native DLL. On the other hand, PInvoke seems like it will take a huge amount of work, if it will work at all. ...
8
by: Rajesh Soni | last post by:
Hi! I'm getting a PInvoke error while trying to execute the following code... declaration: Structure POINTAPI Dim x As IntPtr
0
by: Benosham | last post by:
I have been playing around with trying to PInvoke GDI+ from C#, I made recently made the transition from C/C++ to C# and I really like the language, however being the old fashioned programmer I am I...
14
by: Mohamed Mansour | last post by:
Hey there, this will be somewhat a long post, but any response is appreciated! I have done many PInvoke in the past from C++ to C#, but I did PInvoke within C# not C++/CLI. Can someone explain...
15
by: Luigi | last post by:
// SecureZeroMem.cpp #include <windows.h> extern "C" __declspec(dllexport) PVOID __stdcall SecureZeroMem(IN PVOID ptr, IN SIZE_T cnt) { return SecureZeroMemory(ptr, cnt); }
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.