首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将pcap dll文件嵌入到c#工程创建的DLL中

将pcap dll文件嵌入到c#工程创建的DLL中
EN

Stack Overflow用户
提问于 2017-08-21 22:13:39
回答 1查看 98关注 0票数 1

我正在尝试创建用户定义的函数,它使用pcapDotNet.Core.Dll文件中定义的类。我的c#代码如下:

代码语言:javascript
复制
using System;
using System.IO;
using System.Collections.Generic;
using PcapDotNet.Core;
using System.Linq;
using System.Runtime.InteropServices;
using RGiesecke.DllExport; //GANESH
using System.Runtime.InteropServices;//GANESH
using System.Reflection;

namespace ObtainingAdvancedInformationAboutInstalledDevices
{
    class Program1
    {

        /*
        static void Main(string[] args)
        {
            Program1 var1 = new Program1();
            var1.checkDevice();
        }*/ 

        public Program1()
        {
            AppDomain.CurrentDomain.AssemblyResolve += ResolveAssembly;
        }

        static Assembly ResolveAssembly(object sender, ResolveEventArgs args)
        {
            String dllName = new AssemblyName(args.Name).Name + ".dll";
            var assem = Assembly.GetExecutingAssembly();
            String resourceName = assem.GetManifestResourceNames().FirstOrDefault(rn => rn.EndsWith(dllName));
            if (resourceName == null) return null; // Not found, maybe another handler will find it
            using (var stream = assem.GetManifestResourceStream(resourceName))
            {
                Byte[] assemblyData = new Byte[stream.Length];
                stream.Read(assemblyData, 0, assemblyData.Length);
                return Assembly.Load(assemblyData);
            }
        }

        //**************************************USER DEFINED FUNCTIONS START*********************
        [DllExport("checkFunction", CallingConvention = CallingConvention.Cdecl)]//GANESH
        public static int checkFunction()
        {

            Program1 worker1 = new Program1();
            worker1.checkDevice();
            return 0;
        }


        void checkDevice()
        {
            // Retrieve the interfaces list
            IList<LivePacketDevice> allDevices = LivePacketDevice.AllLocalMachine;
            Console.WriteLine(" inside checkDevice\n");


            // Scan the list printing every entry
            for (int i = 0; i != allDevices.Count(); ++i)
                DevicePrint(allDevices[i]);

        }


        // Print all the available information on the given interface
        private static void DevicePrint(IPacketDevice device)
        {
            // Name
            Console.WriteLine(device.Name);

            // Description
            if (device.Description != null)
                Console.WriteLine("\tDescription: " + device.Description);

            // Loopback Address
            Console.WriteLine("\tLoopback: " +
                              (((device.Attributes & DeviceAttributes.Loopback) == DeviceAttributes.Loopback)
                                   ? "yes"
                                   : "no"));

            // IP addresses
            foreach (DeviceAddress address in device.Addresses)
            {
                Console.WriteLine("\tAddress Family: " + address.Address.Family);

                if (address.Address != null)
                    Console.WriteLine(("\tAddress: " + address.Address));
                if (address.Netmask != null)
                    Console.WriteLine(("\tNetmask: " + address.Netmask));
                if (address.Broadcast != null)
                    Console.WriteLine(("\tBroadcast Address: " + address.Broadcast));
                if (address.Destination != null)
                    Console.WriteLine(("\tDestination Address: " + address.Destination));
            }
            Console.WriteLine();
        }
    }
}

我的python代码如下:

代码语言:javascript
复制
class gooseDriver():
    """A class that creates windows form by importing IED.MainWindow()"""

    def __init__(self, ipAddress, port):

        self.hllDll = WinDLL (r"C:\WORK\IEC61850\gooseThread1\gooseThread1\bin\x64\Debug\gooseThread1.dll")       
        self.hllDll.checkFunction()

        print("Goose Message Started\n")   


def main():
    IED = gooseDriver("192.168.0.20", 102)
    print("GooseDriver is called\n")


if __name__ == '__main__':
    main()

当我试图从python中调用checkFunction时,给出的错误是"OSError: WinError -532462766 Windows error 0xe0434352“。这是因为函数使用的是来自pcap文件的LivePacketsDevice类。我在生成动态链接库的时候嵌入了pcapDotNet.Core.Dll文件作为参考。有谁能建议一下这个问题的解决方案。

EN

回答 1

Stack Overflow用户

发布于 2017-08-30 00:50:14

经过多次试验,当我将Python文件放到pcapDotNet解释器所在的文件夹中时,它开始工作了。不知道什么是理由?有没有人知道这件事。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45799310

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档