PInvoke: where is SecureZeroMemory?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Luigi

    PInvoke: where is SecureZeroMemory?

    SecureZeroMemor y is an alias of RtlSecureZeroMe mory, 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 SecureZeroMemor y RtlSecureZeroMe mory

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

    [DllImport("kern el32.dll", CharSet = CharSet.Auto)]
    private static extern void SecureZeroMemor y(IntPtr ptr, IntPtr cnt);

    Can you help me?

    Thanks, Luigi.
  • Nicholas Paldino [.NET/C# MVP]

    #2
    Re: PInvoke: where is SecureZeroMemor y?

    Luigi,

    Well, SecureZeroMemor y 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 SecureZeroMemor y, you can set the EntryPoint
    property on the DllImport attribute to "RtlSecureZeroM emory":

    [DllImport("kern el32.dll", CharSet = CharSet.Auto, EntryPoint =
    "RtlSecureZeroM emory")]
    private static extern void SecureZeroMemor y(IntPtr ptr, IntPtr cnt);


    --
    - Nicholas Paldino [.NET/C# MVP]
    - mvp@spam.guard. caspershouse.co m

    "Luigi" <newsgroupusr@g mail.comwrote in message
    news:594d9267-740d-4130-9c90-c89a957fd88c@j2 2g2000hsf.googl egroups.com...
    SecureZeroMemor y is an alias of RtlSecureZeroMe mory, 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 SecureZeroMemor y RtlSecureZeroMe mory
    >
    However in C# I want to call SecureZeroMemor y to zero a memory area
    containing a password, but using user32.dll, kernel32.dll or
    advapi32.dll with DllImport setting SecureZeroMemor y or
    RtlSecureZeroMe mory as the entry point an exception says unable to
    find entrypoint X in DLL Y.
    >
    [DllImport("kern el32.dll", CharSet = CharSet.Auto)]
    private static extern void SecureZeroMemor y(IntPtr ptr, IntPtr cnt);
    >
    Can you help me?
    >
    Thanks, Luigi.

    Comment

    • Luigi

      #3
      Re: PInvoke: where is SecureZeroMemor y?

      [DllImport("kern el32.dll", CharSet = CharSet.Auto, EntryPoint = "RtlSecureZeroM emory")]
      private static extern void SecureZeroMemor y(IntPtr ptr, IntPtr cnt);
      >
      Nicholas, I'm running VS 2005 SP1on a Windows XP SP3 machine and using
      the code above I get an EntryPointNotFo undException :(

      Comment

      • Nicholas Paldino [.NET/C# MVP]

        #4
        Re: PInvoke: where is SecureZeroMemor y?

        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 RtlSecureZeroMe mory 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]
        - mvp@spam.guard. caspershouse.co m

        "Luigi" <newsgroupusr@g mail.comwrote in message
        news:23d98fdf-e307-44c1-9190-f129a7b2a300@y2 1g2000hsf.googl egroups.com...
        >[DllImport("kern el32.dll", CharSet = CharSet.Auto, EntryPoint =
        >"RtlSecureZero Memory")]
        >private static extern void SecureZeroMemor y(IntPtr ptr, IntPtr cnt);
        >>
        Nicholas, I'm running VS 2005 SP1on a Windows XP SP3 machine and using
        the code above I get an EntryPointNotFo undException :(

        Comment

        • Giovanni Dicanio

          #5
          Re: PInvoke: where is SecureZeroMemor y?


          "Nicholas Paldino [.NET/C# MVP]" <mvp@spam.guard .caspershouse.c omha
          scritto nel messaggio news:u4W5vKq4IH A.4696@TK2MSFTN GP02.phx.gbl...
          Luigi,
          >
          Well, SecureZeroMemor y 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 SecureZeroMemor y, you can set the EntryPoint
          property on the DllImport attribute to "RtlSecureZeroM emory":
          >
          [DllImport("kern el32.dll", CharSet = CharSet.Auto, EntryPoint =
          "RtlSecureZeroM emory")]
          private static extern void SecureZeroMemor y(IntPtr ptr, IntPtr cnt);
          It seems that there is no RtlSecureZeroMe mory exported from kernel32.dll.

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

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

          Giovanni



          Comment

          • Larry Smith

            #6
            Re: PInvoke: where is SecureZeroMemor y?


            "Luigi" <newsgroupusr@g mail.comwrote in message
            news:23d98fdf-e307-44c1-9190-f129a7b2a300@y2 1g2000hsf.googl egroups.com...
            >[DllImport("kern el32.dll", CharSet = CharSet.Auto, EntryPoint =
            >"RtlSecureZero Memory")]
            >private static extern void SecureZeroMemor y(IntPtr ptr, IntPtr cnt);
            >>
            Nicholas, I'm running VS 2005 SP1on a Windows XP SP3 machine and using
            the code above I get an EntryPointNotFo undException :(
            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.


            Comment

            • Luigi

              #7
              Re: PInvoke: where is SecureZeroMemor y?

              From VS 2003 to VS 2008 SecureZeroMemor y is declared in winbase.h as
              an alias of RtlSecureZeroMe mory 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(dlle xport) PVOID SecureZeroMem(I N PVOID ptr, IN SIZE_T cnt)
              {
              // PVOID SecureZeroMemor y(IN PVOID ptr, IN SIZE_T cnt) is declared
              in winBase.h as an alias for
              // PVOID RtlSecureZeroMe mory(IN PVOID ptr, IN SIZE_T cnt) defined
              in winnt.h as an inline function:

              return SecureZeroMemor y(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("Secu reZeroMem.dll", EntryPoint = "SecureZeroMem" , CharSet
              = CharSet.Auto)]
              private static extern void SecureZeroMemor y(IntPtr ptr, IntPtr cnt);

              Debugging my C# project I get an EntryPointNotFo undException :(

              Could you tell me what is wrong?

              Thanks, Luigi.

              Comment

              • Larry Smith

                #8
                Re: PInvoke: where is SecureZeroMemor y?

                Could you tell me what is wrong?

                Try:

                extern "C" __declspec(dlle xport) PVOID SecureZeroMem(I N PVOID ptr, IN SIZE_T
                cnt)
                {
                // ...
                }

                Also, why do you need the "EntryPoint " clause. Just call it using the
                original name.


                Comment

                • Jeroen Mostert

                  #9
                  Re: PInvoke: where is SecureZeroMemor y?

                  Luigi wrote:
                  From VS 2003 to VS 2008 SecureZeroMemor y is declared in winbase.h as
                  an alias of RtlSecureZeroMe mory 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(dlle xport) PVOID SecureZeroMem(I N PVOID ptr, IN SIZE_T cnt)
                  {
                  // PVOID SecureZeroMemor y(IN PVOID ptr, IN SIZE_T cnt) is declared
                  in winBase.h as an alias for
                  // PVOID RtlSecureZeroMe mory(IN PVOID ptr, IN SIZE_T cnt) defined
                  in winnt.h as an inline function:
                  >
                  return SecureZeroMemor y(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("Secu reZeroMem.dll", EntryPoint = "SecureZeroMem" , CharSet
                  = CharSet.Auto)]
                  private static extern void SecureZeroMemor y(IntPtr ptr, IntPtr cnt);
                  >
                  Debugging my C# project I get an EntryPointNotFo undException :(
                  >
                  Could you tell me what is wrong?
                  >
                  Should be declared like this:

                  extern "C" __declspec(dlle xport) PVOID __stdcall SecureZeroMem(I N 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("Secu reZeroMem.dll", EntryPoint = "SecureZeroMem" )]
                  private static extern IntPtr SecureZeroMemor y(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.

                  Comment

                  • Luigi

                    #10
                    Re: PInvoke: where is SecureZeroMemor y?

                    // SecureZeroMem.c pp

                    #include <windows.h>

                    extern "C" __declspec(dlle xport) PVOID __stdcall SecureZeroMem(I N
                    PVOID ptr, IN SIZE_T cnt)
                    {
                    return SecureZeroMemor y(ptr, cnt);
                    }


                    // Caller.cs

                    [DllImport("Secu reZeroMem.dll", CharSet = CharSet.Auto, SetLastError =
                    true)]
                    private static extern IntPtr SecureZeroMem(I ntPtr ptr, uint cnt);


                    I solved all my problems with the above code (IntPtr cnt generates an
                    unhandled exception).


                    Thank you all!

                    Luigi.

                    Comment

                    Working...