using System.Runtime.InteropServices; // DllImport所在的名字空间
[StructLayout(LayoutKind.Sequential)]
public struct SYSTEM_POWER_STATUS
{
public byte ACLineStatus;
public byte BatteryFlag;
public byte BatteryLifePercent;
public byte Reserved1;
public int BatteryLifeTime;
public int BatteryFullLifeTime;
}
[DllImport("Kernel32")]
public static extern bool GetSystemPowerStatus(out SYSTEM_POWER_STATUS systemPowerStatus);
从c++ dll传一个string给c#
c#:
[DllImport("DllForCSharp", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
public static extern void GetServiceTagByFirmwareVariable(StringBuilder tag);
void UpdateServiceTag()
{
StringBuilder tag = new StringBuilder();
GetServiceTagByFirmwareVariable(tag);
this.labelServiceTagValue.Text = tag.ToString();
}
c++:
DLLFORCSHARP_API void GetServiceTagByFirmwareVariable(LPSTR tag)
{
strcpy(tag, "abcdefg");
}