在注入的本机DLL中使用C#库. [英] Using a C# library in an injected native DLL.

查看:109
本文介绍了在注入的本机DLL中使用C#库.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个注入的C ++ DLL,并且我正在尝试使用C#创建一个插件系统. (我希望能够将C#库作为插件加载,并让C#代码触发本机(有点像包装器),或者可能触发包装的函数,以使这项工作所需的一切.)

GTA IV Script Hook .Net正是这样做的,所以,我知道这是可能的,但是,我很难让C ++ \ C#正常播放.

我实际上是一个纯粹的本机C ++ DLL,我创建了一个.cpp \ .h组合,该组合使用CLR编译.

I have an injected C++ DLL, and, I''m trying to create a plugin system using C#. (I want to be able to load C# libs as plugins, and have the C# code trigger natives, kinda like a wrapper, or maybe trigger wrapped functions, whatever it takes to make this work.)

GTA IV Script Hook .Net, does exactly this, so, I know it''s possible, but, I''m having trouble getting C++\C# to play nicely.

I have what is essentially a pure native C++ DLL, I created one .cpp\.h combo, which is compiled with CLR.

#using <mscorlib.dll>
#using <HackNet.dll>
#include "DotNetPlugins.h"

using namespace HackNet;

void ExecuteNet()
{
    General::PrintFile("NetTest.log", "Net Test...");
}


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace HackNet
{
    public class General
    {
        // Print File
        public static void PrintFile(string path, string text)
        {
            System.IO.File.WriteAllText(path, text);
        }
    }
}



我的CLR cpp \ h文件和C#lib均基于本文.

http://msdn.microsoft.com/en-us/magazine/cc188703.aspx [ ^ ]

但是,它在启动时拒绝工作(C ++运行时错误R6033),告诉我我的应用程序中有一个错误,我试图从构造函数或DLLMain中运行它,但都不是真的. (调用被注入到一个功能,直到我按下一个键,该功能才被调用,并且直到应用程序完全加载并且考虑到它甚至无法启动时,该功能才起作用.)

因此,我哪里出错了,我知道这是可能的,我可以命名两个执行此操作的项目,不幸的是,没有可用的源代码来演示它是如何完成的. (我的本机DLL当然可以正常工作,没有CLR东西,等等.)



My CLR cpp\h files, and C# lib are based on this article.

http://msdn.microsoft.com/en-us/magazine/cc188703.aspx[^]

But, it refuses to work(C++ runtime error R6033) on startup, telling me that I have a bug in my application, that I attempted to run it from a constructor, or DLLMain, neither of which is true. (The call is injected to a function that isn''t invoked until I press a key, and that won''t even work until the app is fully loaded, and considering it won''t even launch..)

So, where am I going wrong, I know this is possible, I can name two projects that do this, unfortunately, no source code is available demonstrating how it was done. (My native DLL of course works fine, without the CLR stuff, etc,.)

推荐答案

回答它,将其弹出未回答的问题列表即可.

OP自己解决了.他的更新:
有趣的是,我将C#库的类类型更改为静态,并重建了我的C ++项目,现在它可以工作了.为了实现我的目标,我相信我需要包装C ++函数,然后对于插件,在C#中加载C ++ \ CLI DLL,使用包装的函数,然后在我的C ++项目中,加载C#库,然后执行它们. (可能不仅限于此,但大致而言,这似乎是正确的.)
Answering it to just pop it out of unanswered question list.

OP solved it by himself. His updates:
Interestingly enough, I changed the class type of the C# lib to static, and rebuilt my C++ project, and now it works... (Not sure what fixed it.) --- I''ve had some thoughts on accomplishing my goal, I believe I need to wrap my C++ functions, then for plugins, load the C++\CLI DLL in C#, use my wrapped functions, then inside my C++ project, load the C# lib, and execute them. (May be more to it than this, but, roughly, this seems about right.)


这篇关于在注入的本机DLL中使用C#库.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆