问题描述
我编写了一个ac#程序,调用一个c + + dll,将命令行参数回应到一个文件。当使用rundll32命令调用c ++时,它会显示命令行没有问题,但是当它从c#中调用时,它不会。
来尝试解决我的问题,但是我已经修改了我的测试环境,我觉得值得一个新的问题。
这是c ++ dll
#includestdafx.h
#includestdlib.h
#include< stdio.h>
#include< iostream>
#include< fstream>
使用命名空间std;
BOOL APIENTRY DllMain(HANDLE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
return TRUE;
}
externC__declspec(dllexport)int WINAPI CMAKEX(
HWND hwnd,
HINSTANCE hinst,
LPCSTR lpszCommandLine,
DWORD dwReserved)
{
ofstream SaveFile(output.txt);
SaveFile<<< lpszCommandLine;
SaveFile.close();
return 0;
}
这是c#应用程序
使用系统;
使用System.Collections.Generic;
使用System.Text;
使用System.Security.Cryptography;
使用System.Runtime.InteropServices;
使用System.Net;
命名空间nac
{
类程序
{
[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 /配置文件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 /配置文件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 /配置文件C:\Documents and Settings\Administrator\Application Data\Microsoft\Network\Connections\Cm\dslvpn.cmp
然而,运行c#应用程序时的输出是
/
code> LPCSTR 不是unicode,是吗?只要使用ANSI,你应该很好: CharSet = CharSet.Ansi
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
/
LPCSTR
is not unicode, is it? Just use ANSI and you should be fine: CharSet = CharSet.Ansi
这篇关于从C#调用非托管DLL。拿2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!