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 preLaunchTask"C/C++: aarch64- apple-darwin22-g++-14 生成活动 文件”已终止,退出代码为-1。
  • ¥18 关于#贝叶斯概率#的问题:这篇文章中利用em算法求出了对数似然值作为概率表参数,然后进行概率表计算,这个概率表是怎样计算的呀
  • ¥20 C#上传XML格式数据
  • ¥15 elementui上传结合oss接口断点续传,现在只差停止上传和继续上传,各大精英看下
  • ¥100 单片机hardfaulr
  • ¥20 手机截图相片分辨率降低一半
  • ¥50 求一段sql语句,遇到小难题了,可以50米解决
  • ¥15 速求,对多种商品的购买力优化问题(用遗传算法、枚举法、粒子群算法、模拟退火算法等方法求解)
  • ¥100 速求!商品购买力最优化问题(用遗传算法求解,给出python代码)
  • ¥15 虚拟机检测,可以是封装好的DLL,可付费