从 C# 调用非托管 dll。拿2
我编写了一个 c# 程序,该程序调用一个 c++ dll,该 dll 将命令行参数回显到文件中。
当使用 rundll32 命令调用 c++ 时,它显示命令行参数没有问题,但是当从 c# 中调用它时,它不会显示。
我问这个问题是为了尝试解决我的问题,但我已经修改了它我的测试环境,我认为值得提出一个新问题。
这是 c++ dll
#include "stdafx.h"
#include "stdlib.h"
#include <stdio.h>
#include <iostream>
#include <fstream>
using namespace std;
BOOL APIENTRY DllMain( HANDLE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
return TRUE;
}
extern "C" __declspec(dllexport) int WINAPI CMAKEX(
HWND hwnd,
HINSTANCE hinst,
LPCSTR lpszCommandLine,
DWORD dwReserved)
{
ofstream SaveFile("output.txt");
SaveFile << lpszCommandLine;
SaveFile.close();
return 0;
}
这是 c# 应用程序
using System;
using System.Collections.Generic;
using System.Text;
using System.Security.Cryptography;
using System.Runtime.InteropServices;
using System.Net;
namespace nac
{
class Program
{
[DllImport("cmakca.dll", SetLastError = true, CharSet = CharSet.Unicode)]
static extern bool CMAKEX(IntPtr hwnd, IntPtr hinst, string lpszCmdLine, int nCmdShow);
static void Main(string[] args)
{
string cmdLine = @"/source_filename proxy-1.txt /backup_filename proxy.bak /DialRasEntry NULL /TunnelRasEntry DSLVPN /profile ""C:\Documents and Settings\Administrator\Application Data\Microsoft\Network\Connections\Cm\dslvpn.cmp""";
const int SW_SHOWNORMAL = 1;
CMAKEX(IntPtr.Zero, IntPtr.Zero, cmdLine, SW_SHOWNORMAL).ToString();
}
}
}
rundll32 命令的输出是,
rundll32 cmakex.dll,CMAKEX /source_filename proxy-1.txt /backup_filename proxy.bak /DialRasEntry NULL /TunnelRasEntry DSLVPN /profile ""C:\Documents and Settings\Administrator\Application Data\Microsoft\Network\Connections\Cm\dslvpn.cmp"
/source_filename proxy-1.txt /backup_filename proxy.bak /DialRasEntry NULL /TunnelRasEntry DSLVPN /profile ""C:\Documents and Settings\Administrator\Application Data\Microsoft\Network\Connections\Cm\dslvpn.cmp"
但是 c# 应用程序运行时的输出是
/
I have written a c# program that calls a c++ dll that echoes the commandline args to a file
When the c++ is called using the rundll32 command it displays the commandline args no problem, however when it is called from within the c# it doesnt.
I asked this question to try and solve my problem, but I have modified it my test environment and I think it is worth asking a new question.
Here is the c++ dll
#include "stdafx.h"
#include "stdlib.h"
#include <stdio.h>
#include <iostream>
#include <fstream>
using namespace std;
BOOL APIENTRY DllMain( HANDLE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
return TRUE;
}
extern "C" __declspec(dllexport) int WINAPI CMAKEX(
HWND hwnd,
HINSTANCE hinst,
LPCSTR lpszCommandLine,
DWORD dwReserved)
{
ofstream SaveFile("output.txt");
SaveFile << lpszCommandLine;
SaveFile.close();
return 0;
}
Here is the c# app
using System;
using System.Collections.Generic;
using System.Text;
using System.Security.Cryptography;
using System.Runtime.InteropServices;
using System.Net;
namespace nac
{
class Program
{
[DllImport("cmakca.dll", SetLastError = true, CharSet = CharSet.Unicode)]
static extern bool CMAKEX(IntPtr hwnd, IntPtr hinst, string lpszCmdLine, int nCmdShow);
static void Main(string[] args)
{
string cmdLine = @"/source_filename proxy-1.txt /backup_filename proxy.bak /DialRasEntry NULL /TunnelRasEntry DSLVPN /profile ""C:\Documents and Settings\Administrator\Application Data\Microsoft\Network\Connections\Cm\dslvpn.cmp""";
const int SW_SHOWNORMAL = 1;
CMAKEX(IntPtr.Zero, IntPtr.Zero, cmdLine, SW_SHOWNORMAL).ToString();
}
}
}
The output from the rundll32 command is
rundll32 cmakex.dll,CMAKEX /source_filename proxy-1.txt /backup_filename proxy.bak /DialRasEntry NULL /TunnelRasEntry DSLVPN /profile ""C:\Documents and Settings\Administrator\Application Data\Microsoft\Network\Connections\Cm\dslvpn.cmp"
/source_filename proxy-1.txt /backup_filename proxy.bak /DialRasEntry NULL /TunnelRasEntry DSLVPN /profile ""C:\Documents and Settings\Administrator\Application Data\Microsoft\Network\Connections\Cm\dslvpn.cmp"
however the output when the c# app runs is
/
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
LPCSTR
不是 unicode,是吗?只需使用 ANSI 就可以了:CharSet = CharSet.Ansi
LPCSTR
is not unicode, is it? Just use ANSI and you should be fine:CharSet = CharSet.Ansi