I'm trying to write a .dll
and then call this .dll
in C#.
Now, I have the problem of how can I convert the unmanaged pointer, thats pointing to an array of COLORREFS
, into a managed array of UInt32
in C#?
Or is there a better approach?
This is the function from the .dll
implemented in a header file:
COLORREF results[10]{ CLR_INVALID }; //<-- First try, dynamic array size is the ultimate goal.
__declspec(dllexport) COLORREF* __stdcall getColorArea(int tlX, int tlY, int brX, int brY) {
HDC _hdc = GetDC(NULL);
if (_hdc)
{
int _x = tlX;
int _y = tlY;
int _count = 0;
while (_x <= brX)
{
if (_y != tlY) { _y = tlY; }
while (_y <= brY)
{
results[_count] = GetPixel(_hdc, _x, _y);
_count++;
_y++;
}
_x++;
}
ReleaseDC(NULL, _hdc);
return results;
}
return results;
}
And here are some parts from the C# source:
[DllImport(@"test.dll", EntryPoint = "getColorArea", CallingConvention = CallingConvention.StdCall)]
unsafe extern static UInt32* getColorArea(int ltX, int ltY, int rbX, int rbY);
unsafe private void GetSetColor(int ltx, int lty, int rbx, int rby) {
int _count = 0;
//Array.Copy(_result, _res, 10); //<-- I tried some variations but nothing worked.
UInt32* _result = getColorArea(ltx, lty, rbx, rby);
foreach (Control _c in this.ColorDisplay.Controls)
{
if (_result[_count] != 0xFFFFFFFF)
{
_c.BackColor = ColorTranslator.FromWin32((int)_result[_count]);
_count++;
}
}
} // It throws no exception but sets in the end only 4 from 10 Controls.
User contributions licensed under CC BY-SA 3.0