0

I have a C# DLL, whose code derives from a base class, which is written in managed C++. (I don't have any control over the base class code)

This base class (which is in managed C++) has a member

int *buffer

is expected to be memset (filled with Zeros) by the derived class (which is in C#). The derived class knows the size of the buffer.

I am using unsafe context, to access the member "int *buffer" of the base class, in the derived class. Please let me know is there any way special way to memset the buffer in "unsafe" context in c#.

I already looked into this What is the equivalent of memset in C#? for details, but I would like to know is there something specifically for "unsafe" context.

Background : This is a conversion project, where the derived class itself was in managed c++ before. Now I am converting the derived class DLL alone to C#. Also I have no control over the base class code! The current code flow is as follows: Only the derived class knows the size of the buffer. The base class creates a memory for that particular size, by getting the size of the buffer from derived, but it doesn't zero fill. The derived class Zero fills it first and then need to appropriately fill the buffer with its contents. Though strange, that is how it is.

Thanks!

Community
  • 1
  • 1
Titus
  • 907
  • 8
  • 18
  • 1
    The code snippet is criminally inadequate, but the simple explanation is that you can't. Because you don't know the size of *buffer*. You are doing it wrong, the point of C++/CLI is to *hide* those implementation details. It should most likely be an `array^` so the C# code sees a managed array. And if you need a pointer to keep unmanaged code happy then you use `pin_ptr`. And using memset() in a C++/CLI program is not a problem. – Hans Passant Jan 18 '15 at 11:50
  • Background : This is a conversion project, where the derived class itself was in managed c++ before. Now I am converting the derived class DLL alone to C#. Also I have no control over the base class code! The code flow is as follows: Only the derived class knows the size of the buffer. The base class creates a memory for that particular size, by getting the size of the buffer from derived, but it doesn't zero fill. The derived class Zerofills it first and then need to appropriately fill the buffer with its contents. – Titus Jan 18 '15 at 12:02

3 Answers3

6

Well, there is... memset. Why settle for a replacement when you can p/invoke the real thing?

[DllImport("msvcrt.dll", EntryPoint = "memset", CallingConvention = CallingConvention.Cdecl, SetLastError = false)]
public static extern IntPtr MemSet(IntPtr dest, int c, IntPtr count);

Taken from pinvoke.net

edit

As @Hans rightfully mentions in the OP comments, this is useless if you don't already know the size of *buffer.

Rotem
  • 21,452
  • 6
  • 62
  • 109
1

You can code it on your own:

void memset( byte* buffer, int value, int size )
{
    for( int i = 0; i < count; i++)
    {
        *( buffer + i ) = value;
    }
}

Or you can use an API for this. Actually RtlZeroMemory sets values to zero. It's not actually memset.

[DllImport("kernel32.dll")]
static extern void RtlZeroMemory(IntPtr dst, int length);

RtlZeroMemory(buffer, bufferLength);
Mustafa Chelik
  • 2,113
  • 2
  • 17
  • 29
  • Your `memset` isn't actually the standard `memset` either. The standard `memset` works on bytes, which changes the meaning of `size`, and if `value` is non-zero, then of `value` as well. –  Jan 18 '15 at 12:10
  • I know man, but OP was asking about `int*`. I'm sure he can handle and change the code as fits his need. But you're right about the `size`, it's a little tricky. Anyway I changed the code. Thank you. – Mustafa Chelik Jan 18 '15 at 14:09
0

RtlZeroMemory is not actually an entry point in kernel32. If yo want something like that, use this in C#

public static unsafe void ZeroMemory(IntPtr Safebuffer, int count)
    {
        if (count == 0) return;
        byte* buffer = (byte*)Safebuffer.ToPointer();
        memset(buffer, count);
    }
    public static unsafe void ZeroMemory(byte* buffer, int count)
    {
        if (count == 0) return;
        while (count-- > 0)
        {
            buffer[count] = 0;
        }
    }
Egbert Nierop
  • 2,066
  • 1
  • 14
  • 16