2016-09-17 2 views
0

어떻게 DLL에서 함수를 호출 할 수 있습니까? 나는 AccessViolationExeception을 가지고있다.dll 메서드를 호출 할 때 액세스 위반 예외

나는 C에 서명이 :

int WINAPI LogonNowait(Context* comm, 
       char* host, 
       char* username, 
       char* password, 
       char* shell_cmd, 
       char* logon_error_msg, 
       int  buflen, 
       SOCKET* sIO, 
       SOCKET* sErr 
      ) 

[DllImport(@"C:\\MyDLL.dll")] 
     public static extern int WaitForLogon(ref Context ctx, ref string errmsg, int bLen, ref int sIO, ref int sErr); 

[StructLayout(LayoutKind.Sequential)] 
    public struct CommContext 
    { 
     public uint inited; 

     public uint rbufsize; 
     public uint rbuf; 
     public uint r_in_container; 
     public uint left_in_container; 
     public uint next_in_container; 


     public uint wbufsize; 
     public uint wbuf; 
     public uint w_in_container; 
     public uint first_free; 
     public uint empty_container; 

     public uint socket; 
     public uint hdrtype; 
     public uint last_error; 
     public uint socket2; 
     public string RX_context; 
     public int port; 
     public uint async_header; 
     public string my_host_addr; 

     public override string ToString() 
     { 
      return port.ToString(); 
     } 
    } 

추가 정보 : 보호 된 메모리를 읽거나 쓰려고했습니다. 이것은 종종 다른 메모리가 손상되었다는 표시입니다. 어떻게 C#에서이 메서드를 올바르게 호출 할 수 있습니까?

답변

-1

나는 이렇게 할 것입니다. 그것이 완전히 정확하지만 가까운 지 확실하지 않습니다. 소켓 게시 설정 오류를 없애기 위해 원본 게시글을 업데이트했습니다. 코드를 실행하기 전에 CommContext를 초기화해야합니다.

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Runtime; 
using System.Runtime.InteropServices; 

namespace ConsoleApplication1 
{ 
    class Program 
    { 
      //int WINAPI LogonNowait(Context* comm, 
      // char* host, 
      // char* username, 
      // char* password, 
      // char* shell_cmd, 
      // char* logon_error_msg, 
      // int  buflen, 
      // SOCKET* sIO, 
      // SOCKET* sErr 
      // ) 
     [DllImport(@"C:\\MyDLL.dll",CallingConvention = CallingConvention.Cdecl)] 
     public static extern int WaitForLogon(IntPtr ctx, IntPtr host, IntPtr username, IntPtr password, IntPtr shell_cmd, IntPtr errmsg, int bLen, IntPtr sIO, IntPtr sErr); 

     [DllImport("ws2_32.dll", CharSet = CharSet.Auto, SetLastError = true)] 
     static extern Int32 WSAStartup(Int16 wVersionRequested, out WSAData wsaData); 

     [DllImport("ws2_32.dll", CharSet = CharSet.Unicode, SetLastError = true)] 
     public static extern IntPtr socket(ADDRESS_FAMILIES_INT af, SOCKET_TYPE_INT socket_type, PROTOCOL_INT protocol); 

     internal enum ADDRESS_FAMILIES_INT : int 
     { 
      /// <summary> 
      /// Unspecified [value = 0]. 
      /// </summary> 
      AF_UNSPEC = 0, 
      /// <summary> 
      /// Local to host (pipes, portals) [value = 1]. 
      /// </summary> 
      AF_UNIX = 1, 
      /// <summary> 
      /// Internetwork: UDP, TCP, etc [value = 2]. 
      /// </summary> 
      AF_INET = 2, 
      /// <summary> 
      /// Arpanet imp addresses [value = 3]. 
      /// </summary> 
      AF_IMPLINK = 3, 
      /// <summary> 
      /// Pup protocols: e.g. BSP [value = 4]. 
      /// </summary> 
      AF_PUP = 4, 
      /// <summary> 
      /// Mit CHAOS protocols [value = 5]. 
      /// </summary> 
      AF_CHAOS = 5, 
      /// <summary> 
      /// XEROX NS protocols [value = 6]. 
      /// </summary> 
      AF_NS = 6, 
      /// <summary> 
      /// IPX protocols: IPX, SPX, etc [value = 6]. 
      /// </summary> 
      AF_IPX = 6, 
      /// <summary> 
      /// ISO protocols [value = 7]. 
      /// </summary> 
      AF_ISO = 7, 
      /// <summary> 
      /// OSI is ISO [value = 7]. 
      /// </summary> 
      AF_OSI = 7, 
      /// <summary> 
      /// european computer manufacturers [value = 8]. 
      /// </summary> 
      AF_ECMA = 8, 
      /// <summary> 
      /// datakit protocols [value = 9]. 
      /// </summary> 
      AF_DATAKIT = 9, 
      /// <summary> 
      /// CCITT protocols, X.25 etc [value = 10]. 
      /// </summary> 
      AF_CCITT = 10, 
      /// <summary> 
      /// IBM SNA [value = 11]. 
      /// </summary> 
      AF_SNA = 11, 
      /// <summary> 
      /// DECnet [value = 12]. 
      /// </summary> 
      AF_DECnet = 12, 
      /// <summary> 
      /// Direct data link interface [value = 13]. 
      /// </summary> 
      AF_DLI = 13, 
      /// <summary> 
      /// LAT [value = 14]. 
      /// </summary> 
      AF_LAT = 14, 
      /// <summary> 
      /// NSC Hyperchannel [value = 15]. 
      /// </summary> 
      AF_HYLINK = 15, 
      /// <summary> 
      /// AppleTalk [value = 16]. 
      /// </summary> 
      AF_APPLETALK = 16, 
      /// <summary> 
      /// NetBios-style addresses [value = 17]. 
      /// </summary> 
      AF_NETBIOS = 17, 
      /// <summary> 
      /// VoiceView [value = 18]. 
      /// </summary> 
      AF_VOICEVIEW = 18, 
      /// <summary> 
      /// Protocols from Firefox [value = 19]. 
      /// </summary> 
      AF_FIREFOX = 19, 
      /// <summary> 
      /// Somebody is using this! [value = 20]. 
      /// </summary> 
      AF_UNKNOWN1 = 20, 
      /// <summary> 
      /// Banyan [value = 21]. 
      /// </summary> 
      AF_BAN = 21, 
      /// <summary> 
      /// Native ATM Services [value = 22]. 
      /// </summary> 
      AF_ATM = 22, 
      /// <summary> 
      /// Internetwork Version 6 [value = 23]. 
      /// </summary> 
      AF_INET6 = 23, 
      /// <summary> 
      /// Microsoft Wolfpack [value = 24]. 
      /// </summary> 
      AF_CLUSTER = 24, 
      /// <summary> 
      /// IEEE 1284.4 WG AF [value = 25]. 
      /// </summary> 
      AF_12844 = 25, 
      /// <summary> 
      /// IrDA [value = 26]. 
      /// </summary> 
      AF_IRDA = 26, 
      /// <summary> 
      /// Network Designers OSI &amp; gateway enabled protocols [value = 28]. 
      /// </summary> 
      AF_NETDES = 28, 
      /// <summary> 
      /// [value = 29]. 
      /// </summary> 
      AF_TCNPROCESS = 29, 
      /// <summary> 
      /// [value = 30]. 
      /// </summary> 
      AF_TCNMESSAGE = 30, 
      /// <summary> 
      /// [value = 31]. 
      /// </summary> 
      AF_ICLFXBM = 31 
     } 

     internal enum SOCKET_TYPE_INT : int 
     { 
      /// <summary> 
      /// stream socket 
      /// </summary> 
      SOCK_STREAM = 1, 

      /// <summary> 
      /// datagram socket 
      /// </summary> 
      SOCK_DGRAM = 2, 

      /// <summary> 
      /// raw-protocol interface 
      /// </summary> 
      SOCK_RAW = 3, 

      /// <summary> 
      /// reliably-delivered message 
      /// </summary> 
      SOCK_RDM = 4, 

      /// <summary> 
      /// sequenced packet stream 
      /// </summary> 
      SOCK_SEQPACKET = 5 
     } 
     internal enum PROTOCOL_INT : int 
     { 
      //dummy for IP 
      IPPROTO_IP = 0, 
      //control message protocol 
      IPPROTO_ICMP = 1, 
      //internet group management protocol 
      IPPROTO_IGMP = 2, 
      //gateway^2 (deprecated) 
      IPPROTO_GGP = 3, 
      //tcp 
      IPPROTO_TCP = 6, 
      //pup 
      IPPROTO_PUP = 12, 
      //user datagram protocol 
      IPPROTO_UDP = 17, 
      //xns idp 
      IPPROTO_IDP = 22, 
      //IPv6 
      IPPROTO_IPV6 = 41, 
      //UNOFFICIAL net disk proto 
      IPPROTO_ND = 77, 

      IPPROTO_ICLFXBM = 78, 
      //raw IP packet 
      IPPROTO_RAW = 255, 

      IPPROTO_MAX = 256 
     } 

     // For 32-bit execution 
     [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)] 
     public struct WSAData 
     { 
      public Int16 version; 
      public Int16 highVersion; 

      [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 257)] 
      public String description; 

      [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 129)] 
      public String systemStatus; 

      public Int16 maxSockets; 
      public Int16 maxUdpDg; 
      public IntPtr vendorInfo; 
     } 

     [StructLayout(LayoutKind.Sequential)] 
     public struct CommContext 
     { 
      public uint inited; 

      public uint rbufsize; 
      public uint rbuf; 
      public uint r_in_container; 
      public uint left_in_container; 
      public uint next_in_container; 


      public uint wbufsize; 
      public uint wbuf; 
      public uint w_in_container; 
      public uint first_free; 
      public uint empty_container; 

      public uint socket; 
      public uint hdrtype; 
      public uint last_error; 
      public uint socket2; 
      public string RX_context; 
      public int port; 
      public uint async_header; 
      public string my_host_addr; 

      public override string ToString() 
      { 
       return port.ToString(); 
      } 
     } 

     static void Main(string[] args) 
     { 
      CommContext cmt = new CommContext(); 
      IntPtr cmtPtr = Marshal.AllocHGlobal(Marshal.SizeOf(cmt)); 
      Marshal.StructureToPtr(cmt, cmtPtr, true); 

      string host = "LocalHost"; 
      byte[] hostBytes = Encoding.UTF8.GetBytes(host + "\0"); 
      IntPtr hostPtr = Marshal.AllocHGlobal(hostBytes.Length); 
      Marshal.Copy(hostBytes, 0, hostPtr, hostBytes.Length); 

      string username = "username"; 
      byte[] usernameBytes = Encoding.UTF8.GetBytes(username + "\0"); 
      IntPtr usernamePtr = Marshal.AllocHGlobal(usernameBytes.Length); 
      Marshal.Copy(usernameBytes, 0, usernamePtr, usernameBytes.Length); 

      string password = "password"; 
      byte[] passwordBytes = Encoding.UTF8.GetBytes(password + "\0"); 
      IntPtr passwordPtr = Marshal.AllocHGlobal(passwordBytes.Length); 
      Marshal.Copy(passwordBytes, 0, passwordPtr, passwordBytes.Length); 

      string shell_cmd = "shell_cmd"; 
      byte[] shell_cmdBytes = Encoding.UTF8.GetBytes(shell_cmd + "\0"); 
      IntPtr shell_cmdPtr = Marshal.AllocHGlobal(shell_cmdBytes.Length); 
      Marshal.Copy(shell_cmdBytes, 0, shell_cmdPtr, shell_cmdBytes.Length); 

      const int BUF_LEN = 256; 
      IntPtr errmsgPtr = new IntPtr(BUF_LEN); 
      int bLen = BUF_LEN; 

      WSAData data = new WSAData(); 
      WSAStartup(0x201, out data); 

      IntPtr sio = socket(ADDRESS_FAMILIES_INT.AF_INET, SOCKET_TYPE_INT.SOCK_STREAM, PROTOCOL_INT.IPPROTO_TCP); 

      IntPtr sErr = socket(ADDRESS_FAMILIES_INT.AF_INET, SOCKET_TYPE_INT.SOCK_STREAM, PROTOCOL_INT.IPPROTO_TCP); 

      int results = WaitForLogon(cmtPtr, hostPtr, usernamePtr, passwordPtr, shell_cmdPtr, errmsgPtr, bLen, sio, sErr); 

      string error = Marshal.PtrToStringAuto(errmsgPtr); 
     } 
    } 
} 
+0

OP 코드에서 관찰 한 문제에 대해 설명하고, 어떤 방식으로 접근했는지, OP가 선택한 방식과 작동 방식에 대해 설명해 주시겠습니까? 이제는 "복사하여 붙여 넣기하면 문제가 해결 될 것"으로 보입니다. 내 downvote 아니지만. – CodeCaster

+0

pinvoke.net에서 발견 된 일부 구조를 사용하여이 코드를 직접 작성했지만 코드는 pinvoke.net에 없습니다. 저는 40 년 동안 K & R "C-Language"를 여러 번 읽었을뿐 아니라 점심 시간에 표지에서 여러 번 커버하기 위해 유닉스 매뉴얼을 읽는 경험을했습니다. 원래 코드의 주된 문제는 포인터를 사용하여 매개 변수를 전달하지 않았고 매개 변수를 관리되는 메모리로 마샬링했습니다. 사용자가 사용할 소켓 유형을 지정하지 않았으므로 ws2_32 소켓을 사용하고 pinvoke.net을 사용하여 정확한 해결책을 안내했습니다. 참조로 사용 된 msdn에 대한 링크 참조를 Pinvoke하십시오. – jdweng

+0

나는 그 의견을 통해 나에게 무엇을 말하려고하는지 전혀 모른다. 내가 요구 한 것은 당신이 당신의 대답을 향상시키는 것이 었습니다. – CodeCaster

관련 문제