help with pinvoke

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • savage

    help with pinvoke

    hey all
    i need to convert an unmanaged c++ dll and header to c#, ive done the header
    structures but having problems with pinvoke signatures

    method 1:
    typedef int (_stdcall *pOmniConnect) (char * IpAddress,
    unsigned int Port,
    unsigned int Timeout,
    unsigned char EncryptionKey[16],
    HWND NotifyWindow);
    IpAddress - ip as ascii string
    Port - udp port as int
    Timeout - in seconds 0-60
    EncryptionKey - 16 byte binary of encryption key. formed from 2, 8 byte hex
    inputs as a single 16 byte binary
    NotifyWindow - window handle that gets reponses

    ive converted that into
    [DllImport("HAI_ NetLib.dll", EntryPoint="_Om niConnect")]
    private static extern int OmniConnect([MarshalAs(Unman agedType.LPStr)]
    string ipAddress, int port, int timeOut, [MarshalAs(Unman agedType.LPStr)]
    Byte encryptionKey, IntPtr hwnd);

    when i test it i get
    'the exception unknown software exception (0x0eedfade) occurred in the
    application at location 0x77e649d3.'

    ive tried changing UnmanagedType and even tried StringBuilder ipAddress, but
    doesnt seem to work.

    any ideas?

    thanks
  • Eyal Safran

    #2
    Re: help with pinvoke

    Hi,

    typedef int (_stdcall *pOmniConnect) (char * IpAddress,
    unsigned int Port,
    unsigned int Timeout,
    unsigned char
    EncryptionKey[16],
    HWND NotifyWindow);
    is a function pointer which is used for callbacks from unmanaged to
    managed.
    which is a delegate in C#

    and it will look like:
    public delegate int OmniConnectEven tHandler(string ipAddress,
    uint
    port,
    uint
    timeout,

    byte[] encryptionKey,

    IntPtr notifyWindow);

    try:
    [DllImport("HAI_ NetLib.dll", EntryPoint="_Om niConnect")]
    private static extern int OmniConnect(str ing ipAddress,
    uint port,
    uint timeOut,
    string
    encryptionKey,
    IntPtr hwnd);

    try adding CallingConventi on=CallingConve ntion.StdCall in the DllImport
    attribute.
    or, if your method is part of a class, then the CallingConventi on
    should be ThisCall and you need to add as the first parameter to your
    OmniConnect signature an "IntPtr instance", where the instance handle
    will passed.

    Can you show how you used the function pointer in your dll? not just
    the function pointer definition, maybe I will have more information for
    you.

    Eyal.

    Comment

    • savage

      #3
      Re: help with pinvoke

      im still lost, now my code looks like this

      [DllImport("HAI_ NetLib.dll", EntryPoint="_Om niConnect",
      CallingConventi on=CallingConve ntion.StdCall)]
      private static extern int OmniConnect(str ing ipAddress, uint port, uint
      timeOut, string encryptionKey, IntPtr hwnd);

      public delegate int OmniConnectEven tHandler(string ipAddress, uint port,
      uint timeOut, byte[] encryptionKey, IntPtr hwnd);

      [DllImport("user 32.dll")]
      static extern IntPtr FindWindow(stri ng lpClassName, string lpWindowName);

      private void button1_click(. ..) {
      IntPtr hWnd = FindWindow(null ,"Form1"); // my test form
      string key = "1010101101.... etc";

      OmniConnect("12 7.0.0.1", 4369, 30, key, hWnd);
      }

      i still get the same error, and how do i make use of that delegate?

      thanks for your help, very much appriciated

      "Eyal Safran" wrote:
      [color=blue]
      > Hi,
      >
      > typedef int (_stdcall *pOmniConnect) (char * IpAddress,
      > unsigned int Port,
      > unsigned int Timeout,
      > unsigned char
      > EncryptionKey[16],
      > HWND NotifyWindow);
      > is a function pointer which is used for callbacks from unmanaged to
      > managed.
      > which is a delegate in C#
      >
      > and it will look like:
      > public delegate int OmniConnectEven tHandler(string ipAddress,
      > uint
      > port,
      > uint
      > timeout,
      >
      > byte[] encryptionKey,
      >
      > IntPtr notifyWindow);
      >
      > try:
      > [DllImport("HAI_ NetLib.dll", EntryPoint="_Om niConnect")]
      > private static extern int OmniConnect(str ing ipAddress,
      > uint port,
      > uint timeOut,
      > string
      > encryptionKey,
      > IntPtr hwnd);
      >
      > try adding CallingConventi on=CallingConve ntion.StdCall in the DllImport
      > attribute.
      > or, if your method is part of a class, then the CallingConventi on
      > should be ThisCall and you need to add as the first parameter to your
      > OmniConnect signature an "IntPtr instance", where the instance handle
      > will passed.
      >
      > Can you show how you used the function pointer in your dll? not just
      > the function pointer definition, maybe I will have more information for
      > you.
      >
      > Eyal.
      >
      >[/color]

      Comment

      • Eyal Safran

        #4
        Re: help with pinvoke



        savage wrote:[color=blue]
        > im still lost, now my code looks like this[/color]

        I'm sorry for that...
        is the cpp code yours? you have the sources?
        where and how in the cpp code do you use the "pOmniConne ct"?
        I mean how is prototype of the OmniConnect function looks like? (not
        the typedef).
        I want to do some tests here, so I need to know a little more about
        your cpp dll.
        [color=blue]
        > public delegate int OmniConnectEven tHandler(string ipAddress, uint port,
        > uint timeOut, byte[] encryptionKey, IntPtr hwnd);[/color]
        disregard the delegate for the meantime... maybe we'll use it.

        Eyal.

        Comment

        • savage

          #5
          Re: help with pinvoke

          the dll is not mine and i dont have the source, but i do have some samples of
          using it in borland c++ and delphi along with the header file and pdf(same as
          header file with abit more explanation).

          ive requested info from the people who created it but its been ages and no
          one got back to me so hopefully you can make sense out of it.

          you can get them here:


          thanks man

          "Eyal Safran" wrote:
          [color=blue]
          >
          >
          > savage wrote:[color=green]
          > > im still lost, now my code looks like this[/color]
          >
          > I'm sorry for that...
          > is the cpp code yours? you have the sources?
          > where and how in the cpp code do you use the "pOmniConne ct"?
          > I mean how is prototype of the OmniConnect function looks like? (not
          > the typedef).
          > I want to do some tests here, so I need to know a little more about
          > your cpp dll.
          >[color=green]
          > > public delegate int OmniConnectEven tHandler(string ipAddress, uint port,
          > > uint timeOut, byte[] encryptionKey, IntPtr hwnd);[/color]
          > disregard the delegate for the meantime... maybe we'll use it.
          >
          > Eyal.
          >
          >[/color]

          Comment

          • Eyal Safran

            #6
            Re: help with pinvoke



            savage wrote:[color=blue]
            > the dll is not mine and i dont have the source, but i do have some samples of
            > using it in borland c++ and delphi along with the header file and pdf(same as
            > header file with abit more explanation).
            >
            > ive requested info from the people who created it but its been ages and no
            > one got back to me so hopefully you can make sense out of it.
            >
            > you can get them here:
            > http://tomuch.com/NetProtocolLib.zip
            >
            > thanks man[/color]

            Listen man, It's working...

            [DllImport("HAI_ NetLib.dll", EntryPoint="_Om niConnect",
            CallingConventi on=CallingConve ntion.StdCall)]
            private static extern int OmniConnect(
            string ipAddress,
            uint port,
            uint timeOut,
            [MarshalAs(Unman agedType.LPStr, SizeConst=16)]
            string encryptionKey,
            IntPtr hwnd);

            you can do it with or without the "[MarshalAs(Unman agedType.LPStr,
            SizeConst=16)]"

            private void button1_Click(o bject sender, System.EventArg s e)
            {
            IntPtr hWnd = FindWindow(null ,"Form1"); // my test form
            string key = "1010101101 ";
            int ret = OmniConnect("19 2.168.0.2", 4096, 30, key, hWnd);
            }

            I even get a return value of 1 which is good...

            Eyal.

            Comment

            • Eyal Safran

              #7
              Re: help with pinvoke

              I even looked with Ethereal and I saw my computer sending udp packets
              to the destination IP & port, I don't know what it was supposed to
              send, but it sent: 0x00, 0x01, 0x01, 0x00 (4 bytes of udp payload)

              Eyal.

              Comment

              • savage

                #8
                Re: help with pinvoke

                strange i rebooted the machine and it returned 1 without errors :)
                now i'll be pulling my hair out trying to figure out how to recieve
                wm_copydata.

                well thank you Eyal for all your help.

                "Eyal Safran" wrote:
                [color=blue]
                > I even looked with Ethereal and I saw my computer sending udp packets
                > to the destination IP & port, I don't know what it was supposed to
                > send, but it sent: 0x00, 0x01, 0x01, 0x00 (4 bytes of udp payload)
                >
                > Eyal.
                >
                >[/color]

                Comment

                Working...