我编写了一个 C# 程序,它调用一个 C++ DLL,将命令行参数回显到文件中
当使用 rundll32 命令调用 c++ 时,它显示命令行参数没有问题,但是当从 c# 内部调用它时,它不会显示。
我问了这个问题 https://stackoverflow.com/questions/2601956/calling-unmanaged-code-from-within-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# 应用程序运行时的输出是
/