【发布时间】:2020-07-22 18:19:18
【问题描述】:
我正在尝试编写 .dll,然后在 C# 中调用此 .dll。我关注了这篇文章:
https://blog.quickbird.uk/calling-c-from-net-core-759563bab75d
现在,我的问题是如何将 COLORREF 中的所有数据返回给 C# 中的调用者,然后才能区分这些值。
这是来自.dll的来源:
#pragma once
#include <windows.h>
extern "C"
{
__declspec(dllexport) COLORREF __stdcall getColor(int X, int Y) {
HDC _hdc = GetDC(NULL);
COLORREF _result = 0;
if (_hdc)
{
_result = GetPixel(_hdc, X, Y);
/* int _red = GetRValue(_result);
int _green = GetGValue(_result);
int _blue = GetBValue(_result); */
ReleaseDC(NULL, _hdc);
return _result;
}
return _result;
}
}
这里有一些来自 C# 源代码的部分:
public partial class Main_Window : Form
{
[DllImport(@"test.dll", EntryPoint = "getColor", CallingConvention = CallingConvention.StdCall)]
private static extern UInt32 getColor(int X, int Y);
private void ColB_Click(object sender, EventArgs e)
{
UInt32 result = getColor(500, 500);
MessageBox.Show(Convert.ToString(result)); //<--- just to show what pops up.
//ColB.BackColor = result; //<--- that didnt work.
}
}
【问题讨论】: