C# sockaddr 到 sockaddr_in [英] C# sockaddr to sockaddr_in

查看:55
本文介绍了C# sockaddr 到 sockaddr_in的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用 C# 编写一个应用程序,它挂钩另一个程序的 connect 函数调用并读取它所连接的 IP.(我知道是 IPv4)

I am writing an application in C# that hooks the connect function call of another program and reads the IP it is connecting to. (I know it is IPv4)

目前它钩子,拦截和转发函数调用,我得到了变量,但是由于 winsock 中的 connect 是用 sockaddr* 变量作为第二个参数调用的,我需要将它转换为 sockaddr_in 以获取 IP 地址.(除非有一种方法可以从 sa_data 字段中检索 ip,但我找不到相关信息.)

Currently it hooks, intercepts and forwards the function call and I get the variables but since connect in winsock is called with a sockaddr* variable as the second argument, I need cast this to a sockaddr_in to get the IP address. (Unless there is a way to retrieve the ip out of the sa_data field, which I couldn't find information about.)

这就是我被卡住的地方.我使用 InteropSignatureToolkit 为结构生成了 C# 代码(生成的代码:http://pastebin.com/twvGw030),但我真的不知道如何将 sockaddr 结构转换为 sockaddr_in.在 C++ 中这会很容易,但我不知道如何在 C# 中做到这一点.

This is where I am stuck. I generated the C# code for the structs using the InteropSignatureToolkit (generated code: http://pastebin.com/twvGw030), but I don't really see how to cast the sockaddr struct to sockaddr_in. In C++ this would be quite easy, but I don't know how to do it in C#.

感谢您花时间阅读本文,希望您能帮助我.:)

Thanks for taking your time to read this and I hope you can help me. :)

推荐答案

在 p/invoke 声明中使用 IntPtr,然后你就可以使用 Marshal.PtrToStructure.先用sockaddr找出sa_family,然后用更具体的类型再次调用Marshal.PtrToStructure.

Use an IntPtr in the p/invoke declaration, then you can use Marshal.PtrToStructure. First use sockaddr to find out sa_family, then call Marshal.PtrToStructure again with the more specific type.

这篇关于C# sockaddr 到 sockaddr_in的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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