O绿缘O
2021-12-11 19:28
采纳率: 100%
浏览 31

各位帮我看看这段C++代码,在VB.net中重写了一份运行时报错System.AccessViolationException:“尝试读取或写入受保护的内存。这通常指示其他内存已损坏。”

问题描述

我只熟悉VB.net以前没有调用过C++的dll,现在需要调用一个dll以下是其头文件,我在VB.net中重写一份,但运行时提示错误,各位帮忙解决一下,十分感谢!

问题相关代码
#ifndef __Yun_H__
#define __Yun_H__

typedef const char*        cpchar;
typedef char*                pchar;
typedef void*                pointer;
typedef const void*        cpointer;
typedef signed char        int8;
typedef unsigned char     uint8;
typedef signed short      int16;
typedef unsigned short    uint16;
typedef signed int        int32;
typedef unsigned int      uint32;
typedef long long         int64;
typedef unsigned long long     uint64;
typedef float                  real32;
typedef double                 real64;
typedef uint32                 yun_result;                    
typedef pointer                window_handle;                
typedef pointer                user_id;                    
typedef pointer                video_handle;                
typedef pointer                audio_handle;                
typedef pointer                talk_handle;                
typedef pointer                alarm_handle;                
typedef pointer                alarm_host_handle;            
typedef pointer                record_handle;                
typedef pointer                find_file_handle;            
typedef pointer                playback_handle;            
typedef pointer                get_file_handle;                
typedef pointer                find_picture_handle;        
typedef pointer                get_picture_handle;            
typedef pointer                search_wifi_handle;            
typedef pointer                get_online_user_handle;    
typedef pointer                upgrade_handle;            
typedef pointer                server_id;                
typedef pointer                tree_handle;                
typedef pointer                node_handle;                
typedef pointer                audio_codec_handle;            
typedef pointer                video_codec_handle;            
typedef pointer                yuv_handle;                    
typedef pointer                bitmap_handle;                
typedef pointer                audio_capture_handle;        
typedef pointer                audio_player_handle;        
typedef pointer                local_record_handle;        
typedef pointer                local_playback_handle;        
typedef pointer                lan_device_search_handle;    
typedef pointer                 push_service_handle;        
typedef pointer                port_handle;                
typedef pointer                listen_handle;                
typedef pointer                get_handle;                    
typedef pointer                user_data;                    

#pragma pack(push)
#pragma pack(1)
typedef struct _LOGIN_SERVER_INFO
{
    cpchar ip;                        
    uint16 port;                    
    cpchar user;                    
    cpchar password;                
    cpchar plat_type;                
    cpchar hard_ver;                
    cpchar soft_ver;                
    uint32 keep_time;                
} LOGIN_SERVER_INFO, *P_LOGIN_SERVER_INFO;

 #define YunCAPI
#ifdef __cplusplus
extern "C" {
#endif

 YunCAPI yun_result Yun_server_connect(P_LOGIN_SERVER_INFO lsi, server_id* hserver, pchar err, int32 err_len);

 #ifdef __cplusplus
}
#endif
以下是我自己写的模块代码:
Public Structure LOGIN_SERVER_INFO
    Public ip As String
    Public port As UShort
    Public user As String
    Public password As String
    Public plat_type As String
    Public hard_ver As String
    Public soft_ver As String
    Public keep_time As UInteger
End Structure

Public Declare Function Yun_server_connect Lib "Yun.dll" (ByRef lsi As LOGIN_SERVER_INFO, ByRef hserver As IntPtr, ByVal err As IntPtr, ByVal err_len As Integer) As UInteger
以下是我的调用:
    Dim Res As UInteger
    Dim svr As Integer

    Dim lsi = New LOGIN_SERVER_INFO() With {
        .ip = addr,
        .port = port,
        .user = usr,
        .password = pwd,
        .plat_type = "pc",
        .hard_ver = "P4",
        .soft_ver = "v1.6"
    }

    Res = Yun_server_connect(lsi, svr, 0, 0)
运行结果及报错内容

Res = Yun_server_connect(lsi, svr, 0, 0)在这里提示错误:System.AccessViolationException:“尝试读取或写入受保护的内存。这通常指示其他内存已损坏。”
各位帮忙看看怎么解决这个问题呀,谢谢了!

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

3条回答 默认 最新

  • Jiachen Li-MSFT 2021-12-14 10:27
    最佳回答

    可能是调用dll动态链接库的时候参数类型错误?
    https://blog.csdn.net/weixin_30698775/article/details/112827939

    评论
    解决 1 无用
    打赏 举报
查看更多回答(2条)