我正在使用一些C++代码,它使用该CreateDIBSection函数来创建位图并返回HBITMAP句柄.将此信息导入.NET程序集的最佳方法是什么?
从终端服务器上运行的Windows服务(在全局空间中),我们希望能够在特定用户的终端服务器会话中启动运行Windows应用程序的进程.
怎么去做这个?
Scenerio:Windows服务在启动时启动.在用户登录到终端服务器用户会话之后,基于仅为Windows服务所知的一些标准,Windows服务希望在运行Windows应用程序的用户会话中启动进程.
例如:我们希望向用户显示"5分钟内关机"警告.Windows服务将检测到这种情况,并在每个用户会话中启动一个进程,启动显示警告的Windows应用程序.而且,是的,我知道还有其他方法可以显示警告对话框,这就是示例,我们想要做的更具侵略性.
我试图使用P/Invokes将llvmc用作C#库(因为我找不到任何.NET绑定).
但是,我有一个问题.llvmc使用char**进行错误传递.
一个例子是这样的:
char* error = NULL;
LLVMVerifyModule(PointerToSomeModule, LLVMAbortProcessAction, &error);
Run Code Online (Sandbox Code Playgroud)
我该怎么做才能在C#代码中使用此函数?
LLVMDisposeMessage(error);
Run Code Online (Sandbox Code Playgroud)
我刚看到答案,认为这可能是一个重要的细节.
我试图在64位.NET程序集上使用DllImport(PInvoke)函数PathCanonicalize,它会导致内存损坏导致各种不良行为(崩溃,异常情况等等).(例如:System.AccessViolationException:尝试读取或写入受保护的内存.这通常表示其他内存已损坏.)
[DllImport("shlwapi", CharSet = CharSet.Auto, EntryPoint="PathCanonicalize", SetLastError = true)]
private static extern bool PathCanonicalize( [Out] StringBuilder lpszDst,[In] string lpszSrc );
public static string MyPathCanonicalize(string path)
{
StringBuilder builder = new StringBuilder();
if (!PathCanonicalize(builder, path))
return path;
return builder.ToString();
}
Run Code Online (Sandbox Code Playgroud)
我在这个帖子中看到我应该使用IntPtr而不是直接字符串.任何人都可以告诉我如何在PathCanonicalize中输入和输出字符串中的那些编组?
原型是:
BOOL PathCanonicalize(
__out LPTSTR lpszDst,
__in LPCTSTR lpszSrc
);
Run Code Online (Sandbox Code Playgroud) 我试图从Windows 7上的C#3.5 Windows服务应用程序使用GetSystemInfo()获取分配粒度大小.但是当从调用返回时,SYSTEM_INFO结构在dwAllocationGranularity中始终为0(其他字段的数据按预期填充) )
为简洁起见,SYSTEM_INFO结构看起来像这样,省略了PROCESSOR_ARCHITECTURE和PROCESSOR_TYPE枚举:
public struct SYSTEM_INFO
{
public PROCESSOR_ARCHITECTURE wProcessorArchitecture;
public ushort wReserved;
public uint dwPageSize;
public int lpMinimumApplicationAddress;
public int lpMaximumApplicationAddress;
public uint dwActiveProcessorMask;
public uint dwNumberOfProcessors;
public PROCESSOR_TYPE dwProcessorType;
public uint dwAllocationGranularity;
public ushort wProcessorLevel;
public ushort wProcessorRevision;
}
Run Code Online (Sandbox Code Playgroud)
对GetSystemInfo的extern调用是这样的:
[DllImport("kernel32")]
public static extern void GetSystemInfo(ref SYSTEM_INFO SystemInfo);
Run Code Online (Sandbox Code Playgroud)
调用代码是这样的:
SYSTEM_INFO sysInfo = new SYSTEM_INFO();
GetSystemInfo(ref sysInfo);
Run Code Online (Sandbox Code Playgroud)
运行代码后的输出SYS_INFO结构是:
dwActiveProcessorMask 4294901759
dwAllocationGranularity 0
dwNumberOfProcessors 2047
dwPageSize 4096
dwProcessorType 15
lpMaximumApplicationAddress 0
lpMinimumApplicationAddress 65536
wProcessorArchitecture 9
wProcessorLevel 4
wProcessorRevision …Run Code Online (Sandbox Code Playgroud) 我有以下Delphi代码:
type
RegbusReq2=packed record
Funct:char;
Device:char;
Device1:char;
Starting:integer;
Quantity:smallint;
_CRC:Word; //CRC
stroka:char;
end;
type
crcReg=packed record
buf:array[0..2] of byte;
value:array[0..5] of byte;
end;
type
myRB=record
case byte of
0:(data:RegbusReq2);
1:(Buff:crcReg);
end;
...
procedure TForm1.Button3Click(Sender: TObject);
var
DataReq:myRB;
Output:array[1..15] of Byte;
i:integer;
nomP:string;
st:string;
begin
cs1.Address:=edit5.Text;
cs1.Port := 6001;
typecon:=2;
DataReq.data.Funct:=chr(63);
DataReq.data.Device:=chr(48);
DataReq.data.Device1:=chr(49);
DataReq.data.Starting:=768;
DataReq.data.Quantity:=7;
DataReq.data._CRC:=CRC2(@DataReq.Buff.value,6);
memo1.Lines.Add(IntToStr(DataReq.data._CRC));
DataReq.data.stroka:=chr(13);
application.ProcessMessages();
cs1.Active:=true;
cs1.Socket.SendBuf(DataReq.data,SizeOf(DataReq.data));
application.ProcessMessages();
cs1.Socket.ReceiveBuf(output,SizeOf(output));
application.ProcessMessages();
cs1.Active:=false;
application.ProcessMessages();
if output[1]<>62 then begin
showmessage('îøèáêà ñâÿçè');
exit;
end;
for i:=10 to 15 do
begin
nomp:= …Run Code Online (Sandbox Code Playgroud) 我创建了一个小样本应用程序来说明我在使用时遇到的问题GetWindowRect.当我单击Foo按钮时,它显示Left返回的值GetWindowRect不同于Window.Left.
看来返回的值Window.Left是相对的,但我不确定是什么.同样奇怪的是,我不能在每台机器上重现这一点,在我的家用笔记本电脑上有或没有额外的显示器我可以重现这个问题,在我的工作电脑上问题不会发生.两个系统都运行Windows 7
为什么这些值不同,我该如何解决这个问题呢?
MainWindow.xaml.cs
using System;
using System.Runtime.InteropServices;
using System.Windows;
using System.Windows.Interop;
namespace PositionTest
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
private IntPtr thisHandle;
public MainWindow()
{
InitializeComponent();
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
thisHandle = new WindowInteropHelper(this).Handle;
}
[DllImport("user32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool GetWindowRect(IntPtr hWnd, ref RECT lpRect);
[StructLayout(LayoutKind.Sequential)]
public struct …Run Code Online (Sandbox Code Playgroud) 使用平台调用时,指定参数[In]是否会影响运行时的方法?
例如,当PInvoking CreateRemoteThread,lpThreadId被指定为out按MSDN上的文章:
HANDLE WINAPI CreateRemoteThread(
_In_ HANDLE hProcess,
_In_ LPSECURITY_ATTRIBUTES lpThreadAttributes,
_In_ SIZE_T dwStackSize,
_In_ LPTHREAD_START_ROUTINE lpStartAddress,
_In_ LPVOID lpParameter,
_In_ DWORD dwCreationFlags,
_Out_ LPDWORD lpThreadId
);
Run Code Online (Sandbox Code Playgroud)
所有其他论点都是_In_.在我的C#代码中,我像这样处理特定的函数:
[DllImport("kernel32.dll", EntryPoint = "CreateRemoteThread", SetLastError = true)]
public static extern IntPtr CreateRemoteThread(
IntPtr hProcess,
IntPtr lpThreadAttributes,
uint dwStackSize,
IntPtr lpStartAddress,
IntPtr lpParameter,
uint dwCreationFlags,
[Out] IntPtr lpThreadId);
Run Code Online (Sandbox Code Playgroud)
添加[Out]属性,lpThreadId以便运行时知道将其封送回调用者.如果我将函数签名更改为此,运行时是否会处理我的函数:
[DllImport("kernel32.dll", EntryPoint = "CreateRemoteThread", SetLastError = true)]
public static extern …Run Code Online (Sandbox Code Playgroud) 通过引用将字段传递给非托管的extern方法是否安全?
[StructLayout(LayoutKind.Sequential)]
struct SomeStruct
{
public int SomeValue;
}
class SomeClass
{
SomeStruct _someStruct;
[DllImport("SomeLibrary.dll")]
static extern void SomeExternMethod(ref SomeStruct someStruct);
public void SomeMethod()
{
// Is this safe or do I have to pin the current instance of SomeClass?
SomeExternMethod(ref _someStruct);
}
}
Run Code Online (Sandbox Code Playgroud) 如何char* []在C#中P /调用
任何一个告诉以后如何继续。我想将参数从C#发送到C ++ DLL。我在Google上搜索了很多网站,但没有解决方案。
C功能
public void* find(char*[] argv)
{
}
Run Code Online (Sandbox Code Playgroud)
C#函数我想用以下参数调用此函数
char *Argv[] = { "Tool", "Sachin", "192.168.1.1", "3", "400"};
Run Code Online (Sandbox Code Playgroud)
提前致谢。