0

Hi guys, I read lot articles about this question but nothing that I tried worked.

Code:

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());
            }
        }

    }
}

I tried to add reference to dll, add it to project, changed Build Action to embedded resource, but nothing worked. Any help?

Giovanni Di Toro
  • 797
  • 1
  • 14
  • 34
user2608247
  • 33
  • 1
  • 4
  • What do you mean by Embedding unmanaged dll? Espacially embedding `user32.dll`? Having it included in the exe file? – Cédric Bignon Jul 22 '13 at 20:28
  • You want to embed xNet.dll as a binary resource in your executable? Or are you trying to call functions that are in xNet.dll? Your question is very confusing. – Jim Mischel Jul 22 '13 at 20:34
  • @ I trying to call functions that are in xNet.dll – user2608247 Jul 22 '13 at 20:48
  • Then you'll have to show us those function prototypes and we can help you create the managed prototypes that allow you to call them from your C# program. – Jim Mischel Jul 22 '13 at 21:49

1 Answers1

0

Let's call the assembly of your project MyAssembly.

Create a new folder at the root of your project in Visual Studio. Let's call it MyDlls.

Put your assembly you want to include in this folder and set their Build Action to Embedded Resource.

Then, in your code, add the following elements:

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);
        }
    }
}
Cédric Bignon
  • 12,892
  • 3
  • 39
  • 51