【问题标题】:How to embed unmanaged dll into console app如何将非托管 dll 嵌入控制台应用程序
【发布时间】:2013-07-21 17:31:19
【问题描述】:

大家好,我阅读了很多关于这个问题的文章,但我尝试过的都没有。

代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.Reflection;
using xNet.Net;
using xNet.Collections;

namespace ConsoleApplication1
{
    class Program
    {
        [DllImport("user32.dll")]
        internal static extern bool OpenClipboard(IntPtr hWndNewOwner);

        [DllImport("user32.dll")]
        internal static extern bool CloseClipboard();

        [DllImport("user32.dll")]
        internal static extern bool SetClipboardData(uint uFormat, IntPtr data);


        [STAThread]
        static void Main(string[] args)
        {

            go();
        }

        public static void go()
        {
            CookieDictionary cookies = new CookieDictionary();
            Console.WriteLine(@"[~] Trying to upload text to http://pastebin.ru/");
            try
            {
                using (var request = new HttpRequest())
                {
                    request.UserAgent = HttpHelper.ChromeUserAgent();
                    request.EnableEncodingContent = true;
                    request.Cookies = cookies;
                    request.AllowAutoRedirect = false;

                    var postData = new RequestParams();
                    postData["parent_pid"] = "";
                    postData["std-x"] = "1440";
                    postData["std-y"] = "900";
                    postData["poster"] = "";
                    postData["code_name"] = "";
                    postData["code"] = @"text";
                    postData["mode"] = "178";
                    postData["private"] = "1";
                    postData["expired"] = "1";
                    postData["paste"] = "Отправить";

                    var response = request.Post("http://pastebin.ru/", postData);
                    var url = response.Location;
                    if (string.IsNullOrEmpty(url))
                    {
                        Console.WriteLine(@"[!] Failed to upload text to http://pastebin.ru/\r\n");
                        Console.ReadKey();
                    }
                    else
                    {
                        url = @"http://pastebin.ru" + url;
                        Console.WriteLine(@"[+] Successfully uploaded to " + url);
                        OpenClipboard(IntPtr.Zero);
                        var ptr = Marshal.StringToHGlobalUni(url);
                        SetClipboardData(13, ptr);
                        CloseClipboard();
                        Marshal.FreeHGlobal(ptr);
                    }
                }
            }
            catch (NetException ex)
            {
                Console.WriteLine("Net error: " + ex.Message.ToString());
            }
        }

    }
}

我尝试添加对 dll 的引用,将其添加到项目中,将 Build Action 更改为嵌入式资源,但没有任何效果。有什么帮助吗?

【问题讨论】:

  • 嵌入非托管 dll 是什么意思?特别嵌入user32.dll?是否将其包含在 exe 文件中?
  • 您想将 xNet.dll 作为二进制资源嵌入到您的可执行文件中吗?或者您是否尝试调用 xNet.dll 中的函数?你的问题很混乱。
  • @我试图调用 xNet.dll 中的函数
  • 那么您必须向我们展示这些函数原型,我们可以帮助您创建托管原型,以便您从 C# 程序中调用它们。

标签: c# dll dllimport embedded-resource


【解决方案1】:

我们将您的项目的程序集称为MyAssembly

在 Visual Studio 中项目的根目录下创建一个新文件夹。我们就叫它MyDlls吧。

将您想要包含的程序集放入此文件夹,并将它们的 Build Action 设置为 Embedded Resource

然后,在您的代码中,添加以下元素:

class Program
{
    // ... Your code

    [STAThread]
    static void Main(string[] args)
    {
        AppDomain.CurrentDomain.AssemblyResolve += AssemblyResolve;  // Called when the assembly hasn't been successfully resolved

        // ... Your code
    }

    private Assembly AssemblyResolve(object sender, ResolveEventArgs args)
    {
        Assembly assembly = Assembly.GetExecutingAssembly();

        string assemblyName = args.Name.Split(',')[0];  // Gets the assembly name to resolve.

        using (Stream stream = assembly.GetManifestResourceStream("MyAssembly.MyDlls." + assemblyName + ".dll"))  // Gets the assembly in the embedded resources
        {
            if (stream == null)
                return null;

            byte[] rawAssembly = new byte[stream.Length];
            stream.Read(rawAssembly, 0, (int)stream.Length); 
            return Assembly.Load(rawAssembly);
        }
    }
}

【讨论】:

    猜你喜欢
    • 2010-10-14
    • 2012-07-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-28
    • 2018-12-30
    相关资源
    最近更新 更多