当前位置:编程学习 > C#/ASP.NET >>

vb.net 调用vc++ 含有结构体的dll出错

最近遇到vb.net 要调用vc++写的含有结构体的dll,调试老报错。郁闷死了,向高手求救。
vc++的dll及结构体结构是:
struct nc_userinfo {
char username[21];
char password[15]; 
UINT dataaccess 
}

struct nc_appinfo{
char appname[256]; 
UINT kind 
}

int nc_app3( HWND hWnd, UINT* logonid, nc_appinfo* appinfo, nc_userinfo* usrinfo, char* accessfile, int* info1) 

我的vb.net是这样调用的:
Imports System.Runtime.InteropServices

<StructLayoutAttribute(LayoutKind.Sequential, CharSet:=CharSet.Ansi)> _
Public Structure nc_appinfo        
    <MarshalAsAttribute(UnmanagedType.ByValTStr, SizeConst:=256)> _
    Public appname As String        
    Public kind As UInteger
End Structure

 <StructLayoutAttribute(LayoutKind.Sequential, CharSet:=CharSet.Ansi)> _
Public Structure nc_userinfo        
    <MarshalAsAttribute(UnmanagedType.ByValTStr, SizeConst:=NC_USER_NAME)> _
    Public username As String
    <MarshalAsAttribute(UnmanagedType.ByValTStr, SizeConst:=NC_PASSWORD)> _
    Public password As String
    Public dataaccess As UInteger
End Structure

<DllImport("mydll", EntryPoint:="nc_app3", CallingConvention:=CallingConvention.Cdecl)> _
Public Function nc_app_start(ByRef hWnd As System.IntPtr, _
                             ByRef logonid As UInteger, _
                             ByRef appinfo As nc_appinfo, _
                             ByRef usrinfo As nc_userinfo, _
                             ByRef accessfile As String, _
                             ByRef info1 As Integer) As Integer        
End Function
调试时老报“System.AccessViolationException”类型的未经处理的异常出现在 mscorlib.dll 中
其他信息: 尝试读取或写入受保护的内存。这通常指示其他内存已损坏。

vb.net vc++ dll structure --------------------编程问答--------------------
<StructLayoutAttribute(LayoutKind.Sequential, CharSet:=CharSet.Ansi)> _
 Public Structure nc_appinfo        
     <MarshalAsAttribute(UnmanagedType.ByValTStr, SizeConst:=256)> _
     Public appname() As byte
     Public kind As UInteger
 End Structure
 
 <StructLayoutAttribute(LayoutKind.Sequential, CharSet:=CharSet.Ansi)> _
 Public Structure nc_userinfo        
     <MarshalAsAttribute(UnmanagedType.ByValTStr, SizeConst:=21)> _
     Public username() As byte
     <MarshalAsAttribute(UnmanagedType.ByValTStr, SizeConst:=15)> _
     Public password() As byte
     Public dataaccess As UInteger
 End Structure
  --------------------编程问答-------------------- gxingmin ,试了,不行 --------------------编程问答-------------------- try
<DllImport("mydll", EntryPoint:="nc_app3", CallingConvention:=CallingConvention.Cdecl)> _
Public Function nc_app_start(ByRef hWnd As System.IntPtr, _
                             ByRef logonid As UInteger, _
                             ByRef appinfo As nc_appinfo, _
                             ByRef usrinfo As nc_userinfo, _
                             ByRef accessfile As String, _
                             ByRef info1 As IntPtr) As Integer        --------------------编程问答-------------------- 主要是要看c++怎么调用的?
--------------------编程问答-------------------- 我刚刚用vb6调用该dll成功了,其中用到vbanycall这个类模块(可以调cdecl的dllaccessfile As String).
vb.net还在测试。
vb6用了十几年,还是能解决一些问题。
不过,我还得想办法搞定vb.net。
重新检查了vb.net代码,发现问题可能处在accessfile As String上,应该改为accessfile As  bybe.
--------------------编程问答-------------------- 终于自己搞定了没有报警了,accessfile 为一个单字符故定义为char

跟大家分享一下:

Imports System.Runtime.InteropServices

'删除该行<StructLayoutAttribute(LayoutKind.Sequential, CharSet:=CharSet.Ansi)>
Public Structure nc_appinfo        
    <MarshalAsAttribute(UnmanagedType.ByValTStr, SizeConst:=256)> _
    Public appname As String        
    Public kind As UInteger
End Structure

'删除该行 [color=#FF0000]<StructLayoutAttribute(LayoutKind.Sequential, CharSet:=CharSet.Ansi)> 
Public Structure nc_userinfo        
    <MarshalAsAttribute(UnmanagedType.ByValTStr, SizeConst:=NC_USER_NAME)> _
    Public username As String
    <MarshalAsAttribute(UnmanagedType.ByValTStr, SizeConst:=NC_PASSWORD)> _
    Public password As String
    Public dataaccess As UInteger
End Structure

<DllImport("mydll", EntryPoint:="nc_app3", CallingConvention:=CallingConvention.Cdecl)> _
Public Function nc_app_start(ByRef hWnd As System.IntPtr, _
                             ByRef logonid As UInteger, _
                             ByRef appinfo As nc_appinfo, _
                             ByRef usrinfo As nc_userinfo, _
                             ByRef accessfile As char, _
                             ByRef info1 As Integer) As Integer        
End Function
--------------------编程问答-------------------- 既然vb.net有char数据类型,那咱就用上。更准确地方法是:


Imports System.Runtime.InteropServices

<StructLayoutAttribute(LayoutKind.Sequential, CharSet:=CharSet.Ansi)> _
Public Structure nc_appinfo        
    <MarshalAsAttribute(UnmanagedType.ByValArray, SizeConst:=256)> _
    Public appname() As Char        
    Public kind As UInteger
End Structure

 <StructLayoutAttribute(LayoutKind.Sequential, CharSet:=CharSet.Ansi)> _
Public Structure nc_userinfo        
    <MarshalAsAttribute(UnmanagedType.ByValArray, SizeConst:=NC_USER_NAME)> _
    Public username() As Char
    <MarshalAsAttribute(UnmanagedType.ByValArray, SizeConst:=NC_PASSWORD)> _
    Public password() As Char
    Public dataaccess As UInteger
End Structure

<DllImport("mydll", EntryPoint:="nc_app3", CallingConvention:=CallingConvention.Cdecl)> _
Public Function nc_app_start(ByRef hWnd As System.IntPtr, _
                             ByRef logonid As UInteger, _
                             ByRef appinfo As nc_appinfo, _
                             ByRef usrinfo As nc_userinfo, _
                             ByRef accessfile As Char, _
                             ByRef info1 As Integer) As Integer        
End Function

补充:.NET技术 ,  VB.NET
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,