C# 我如何与C++;C语言中的NPAPI动态链接库# 我有一个开源的C++ DLL

C# 我如何与C++;C语言中的NPAPI动态链接库# 我有一个开源的C++ DLL ,c#,dllimport,npapi,C#,Dllimport,Npapi,实际上,它是一个来自CRX扩展的插件dll,我正试图在VisualStudio中用C#调用它的函数 这个扩展是一个谷歌浏览器屏幕截图 我设法创建了与DLL对话的代码,但我不知道如何调用它的函数 这是我的代码: using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Runtime.InteropServices; namespace ConsoleApp

实际上,它是一个来自CRX扩展的插件dll,我正试图在VisualStudio中用C#调用它的函数

这个扩展是一个谷歌浏览器屏幕截图

我设法创建了与DLL对话的代码,但我不知道如何调用它的函数

这是我的代码:

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

namespace ConsoleApplication1
{
    class Program
    {
        [StructLayout(LayoutKind.Sequential, Pack = 1)]
        private class Sample
        {
            public Int32 length;
            public String value;
        }

        [DllImport("C:\\Users\\Ofir\\Documents\\Visual Studio 2010\\Projects\\ConsoleApplication1\\ConsoleApplication1\\bin\\Debug\\screen_capture.dll")]
        private static extern void NP_Initialize();

        static void Main(string[] args)
        {
            Sample s = new Sample();
            s.length = 0;
            s.value = "Huhu";
            NP_Initialize(); <-- I get an ERROR here : 
        }
    }
}
但我不知道如何建立这种类型和发送它。 我想完成在IE工具栏中构建一个函数,然后使用这个DLL中的函数。 这样我就可以在IE中使用屏幕截图了

EDIT2:调用NP\u Shutdown()函数时,一切正常。 一切都很清楚,没有例外。 所以我猜这都是我发送给另一个函数的类型。
但是我该如何发送这种类型的邮件呢?

这是一个老问题,但我最近对NPAPI做了一些小调查,所以我分享了我的发现。虽然我无法测试您的案例,但我会走这条路。
首先声明一个
\u NPPluginFuncs
结构:

[StructLayout(LayoutKind.Sequential, Pack = 1)]
struct _NPPluginFuncs
{
    UInt16 size;
    UInt16 version;
    IntPtr newp;
    IntPtr destroy;
    IntPtr setwindow;
    IntPtr newstream;
    IntPtr destroystream;
    IntPtr asfile;
    IntPtr writeready;
    IntPtr write;
    IntPtr print;
    IntPtr @event;
    IntPtr urlnotify;
    IntPtr javaClass;
    IntPtr getvalue;
    IntPtr setvalue;
    IntPtr gotfocus;
    IntPtr lostfocus;
    IntPtr urlredirectnotify;
    IntPtr clearsitedata;
    IntPtr getsiteswithdata;
    IntPtr didComposite;
}
然后声明PInvoke调用:

[System.Runtime.InteropServices.DllImport("C:\\Users\\Ofir\\Documents\\Visual Studio 2010\\Projects\\ConsoleApplication1\\ConsoleApplication1\\bin\\Debug\\screen_capture.dll")]
private static extern void NP_Initialize(ref _NPPluginFuncs nPPluginFuncs);

[System.Runtime.InteropServices.DllImport("C:\\Users\\Ofir\\Documents\\Visual Studio 2010\\Projects\\ConsoleApplication1\\ConsoleApplication1\\bin\\Debug\\screen_capture.dll")]
private static extern IntPtr NP_GetEntryPoints(ref _NPPluginFuncs nPPluginFuncs);

[System.Runtime.InteropServices.DllImport("C:\\Users\\Ofir\\Documents\\Visual Studio 2010\\Projects\\ConsoleApplication1\\ConsoleApplication1\\bin\\Debug\\screen_capture.dll")]
private static extern void NP_Shutdown();
最后,调用NPAPI:

_NPPluginFuncs nPPluginFuncs = new _NPPluginFuncs();
NP_Initialize(ref nPPluginFuncs);   // all members are 0 (IntPtr.Zero) after the call
IntPtr res = NP_GetEntryPoints(ref nPPluginFuncs);  // memory addresses filled in
NP_Shutdown();

你能确认这对你是否有效吗?还有另一个大任务摆在你面前:调用那些
IntPtr
函数,这些函数返回了
NP\u GetEntryPoints

这是一个老问题,但我最近对NPAPI做了一些小的探索,所以我分享了我的发现。虽然我无法测试您的案例,但我会走这条路。
首先声明一个
\u NPPluginFuncs
结构:

[StructLayout(LayoutKind.Sequential, Pack = 1)]
struct _NPPluginFuncs
{
    UInt16 size;
    UInt16 version;
    IntPtr newp;
    IntPtr destroy;
    IntPtr setwindow;
    IntPtr newstream;
    IntPtr destroystream;
    IntPtr asfile;
    IntPtr writeready;
    IntPtr write;
    IntPtr print;
    IntPtr @event;
    IntPtr urlnotify;
    IntPtr javaClass;
    IntPtr getvalue;
    IntPtr setvalue;
    IntPtr gotfocus;
    IntPtr lostfocus;
    IntPtr urlredirectnotify;
    IntPtr clearsitedata;
    IntPtr getsiteswithdata;
    IntPtr didComposite;
}
然后声明PInvoke调用:

[System.Runtime.InteropServices.DllImport("C:\\Users\\Ofir\\Documents\\Visual Studio 2010\\Projects\\ConsoleApplication1\\ConsoleApplication1\\bin\\Debug\\screen_capture.dll")]
private static extern void NP_Initialize(ref _NPPluginFuncs nPPluginFuncs);

[System.Runtime.InteropServices.DllImport("C:\\Users\\Ofir\\Documents\\Visual Studio 2010\\Projects\\ConsoleApplication1\\ConsoleApplication1\\bin\\Debug\\screen_capture.dll")]
private static extern IntPtr NP_GetEntryPoints(ref _NPPluginFuncs nPPluginFuncs);

[System.Runtime.InteropServices.DllImport("C:\\Users\\Ofir\\Documents\\Visual Studio 2010\\Projects\\ConsoleApplication1\\ConsoleApplication1\\bin\\Debug\\screen_capture.dll")]
private static extern void NP_Shutdown();
最后,调用NPAPI:

_NPPluginFuncs nPPluginFuncs = new _NPPluginFuncs();
NP_Initialize(ref nPPluginFuncs);   // all members are 0 (IntPtr.Zero) after the call
IntPtr res = NP_GetEntryPoints(ref nPPluginFuncs);  // memory addresses filled in
NP_Shutdown();

你能确认这对你是否有效吗?您面前还有另一个大任务:调用
IntPtr
返回的
NP\u GetEntryPoints
函数…

是否
NP\u Initialize
应该接受参数,如果在期望的时候不传递,肯定会使堆栈失衡。您可以将
CallingConvention
DllImport
中删除-这些函数被声明为
WINAPI
,这意味着默认的调用约定很好,事实上使用
Cdecl
会给您带来问题。当然,如果您真的不知道
的话,那么最好的开始就是。如果您需要有关其特定部分的帮助,请更新您的问题。感谢您的评论,我的问题是生成此_NPPluginFuncs对象并将其发送到函数…是否
NP\u Initialize
应该接受参数,如果在期望的时候不传递,肯定会使堆栈失衡。您可以将
CallingConvention
DllImport
中删除-这些函数被声明为
WINAPI
,这意味着默认的调用约定很好,事实上使用
Cdecl
会给您带来问题。当然,如果您真的不知道
的话,那么最好的开始就是。如果您需要关于它的特定部分的帮助,请更新您的问题。感谢您的评论,我的问题是使这个_NPPluginFuncs对象并将其发送到函数…感谢您Peter Ivan的回答。我将尝试确认这一点(这是我的一个老项目),但你应该得到一些+1只是为了这个干净和有用的答案!嘿,彼得。我遵循这段代码,从NP_GetEntryPoints获取指针。我该如何调用我的插件函数呢?@Tsury:也许可以帮你。谢谢你Peter Ivan的回答。我将尝试确认这一点(这是我的一个老项目),但你应该得到一些+1只是为了这个干净和有用的答案!嘿,彼得。我遵循这段代码,从NP_GetEntryPoints获取指针。我该如何调用我的插件函数呢?@Tsury:也许可以帮你。