O绿缘O 2021-12-11 19:28 采纳率: 100%
浏览 41
已结题

各位帮我看看这段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条回答 默认 最新

  • 丨Haruna 2021-12-14 10:27
    关注

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 12月14日
  • 已采纳回答 12月14日
  • 修改了问题 12月13日
  • 修改了问题 12月11日
  • 展开全部

悬赏问题

  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料