从 C# 调用非托管 dll。拿2

发布于 2024-08-28 16:50:33 字数 2303 浏览 2 评论 0原文

我编写了一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

潦草背影 2024-09-04 16:50:34

LPCSTR 不是 unicode,是吗?只需使用 ANSI 就可以了:CharSet = CharSet.Ansi

LPCSTR is not unicode, is it? Just use ANSI and you should be fine: CharSet = CharSet.Ansi

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文