本文介绍了调用从C#非托管的DLL。取2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了调用一个C ++ DLL呼应命令行args设置为一个文件



在C ++的使用RUNDLL32命令调用它显示的命令行C#程序ARGS没有问题,但是当它被从C#它不中调用。



的,试图解决我的问题,但我已经修改了它我的测试环境,我认为这是值得一问的新问题。



下面是C ++ DLL

 的#includestdafx.h中
#包括stdlib.h中
&#包括LT; stdio.h中>
&#包括LT; iostream的>
&#包括LT; fstream的>
使用命名空间std;

BOOL APIENTRY的DllMain(HANDLE HMODULE,
DWORD ul_reason_for_call,
LPVOID lpReserved

{
返回TRUE;
}

的externC__declspec(dllexport)的INT WINAPI CMAKEX(
HWND HWND,
HINSTANCE HINST,
LPCSTR lpszCommandLine,
DWORD dwReserved)
{

的ofstream SAVEFILE(output.txt的);
SAVEFILE<< lpszCommandLine;
SaveFile.close();

返回0;
}

下面是C#应用程序

 使用系统; 
使用System.Collections.Generic;
使用System.Text;
使用System.Security.Cryptography;使用System.Runtime.InteropServices
;使用System.Net
;

命名空间NAC
{
类节目
{
函数[DllImport(cmakca.dll,SetLastError = true时,字符集= CharSet.Unicode)
静态外部布尔CMAKEX(IntPtr的HWND,IntPtr的HINST,串lpszCmdLine,诠释的nCmdShow);

静态无效的主要(字串[] args)
{
串CMDLINE = @/ source_filename代理的1.txt / backup_filename proxy.bak / DialRasEntry NULL / TunnelRasEntry DSLVPN /配置文件C:\Documents和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代理的1.txt / backup_filename proxy.bak / DialRasEntry NULL / TunnelRasEntry DSLVPN /配置文件C:\Documents和Settings\Administrator\Application Data\Microsoft\Network\Connections\Cm\dslvpn.cmp

/ source_filename代理的1.txt / backup_filename proxy.bak / DialRasEntry NULL / TunnelRasEntry DSLVPN / profile文件C:\Documents和Settings\Administrator\Application Data\Microsoft\Network\Connections\Cm\dslvpn.cmp

然而,当C#应用程序运行输出

  / 


解决方案

LPCSTR 不是Unicode,是吗?只需使用ANSI,你应该罚款:字符集= 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-23 12:27