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日
  • 展开全部

悬赏问题

  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)