import dll c++ function

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

    import dll c++ function

    Any body knows how to convert this DLL function to C# ?

    DWORD WINAPI
    importfunction( LPSTR lpBuffer, LPDWORD nSize);

  • Alan Pretre

    #2
    Re: import dll c++ function

    "centrino" <centrino@discu ssions.microsof t.com> wrote in message
    news:459EB6EC-2AC5-4848-92E2-D963EAF58353@mi crosoft.com...[color=blue]
    > Any body knows how to convert this DLL function to C# ?
    >
    > DWORD WINAPI
    > importfunction( LPSTR lpBuffer, LPDWORD nSize);[/color]

    [System.Runtime. InteropServices .DllImport(my.d ll)]
    static extern System.Int32
    importfunction([System.Runtime. InteropServices .MarshalAs(Syst em.Runtime.Inte ropServices.Unm anagedType.LPSt r)]
    string lpBuffer,
    System.IntPtr nSize
    );


    If this is from a Win32 function you can probably find it at



    -- Alan


    Comment

    • Willy Denoyette [MVP]

      #3
      Re: import dll c++ function


      "centrino" <centrino@discu ssions.microsof t.com> wrote in message
      news:459EB6EC-2AC5-4848-92E2-D963EAF58353@mi crosoft.com...[color=blue]
      > Any body knows how to convert this DLL function to C# ?
      >
      > DWORD WINAPI
      > importfunction( LPSTR lpBuffer, LPDWORD nSize);
      >[/color]


      [DllImport("your .dll", CallingConventi on = CallingConventi on.StdCall)]
      static extern importfunction
      (
      [MarshalAs(Unman agedType.LPTStr )]
      string lpBuffer,
      ref int nSize
      );

      Willy.



      Comment

      • centrino

        #4
        Re: import dll c++ function

        I don't know why i still get a empty buffer :

        there is a other similar function:

        DWORD WINAPI importfunction(
        LPWSTR lpBuffer,
        LPDWORD nSize
        );


        [DllImport("mydl l", SetLastError=tr ue)]
        private static extern uint importfunction(

        [MarshalAs(Unman agedType.LPWStr )]
        StringBuilder pBuffer,
        ref uint pBufferSize
        );

        "Willy Denoyette [MVP]" wrote:
        [color=blue]
        >
        > "centrino" <centrino@discu ssions.microsof t.com> wrote in message
        > news:459EB6EC-2AC5-4848-92E2-D963EAF58353@mi crosoft.com...[color=green]
        > > Any body knows how to convert this DLL function to C# ?
        > >
        > > DWORD WINAPI
        > > importfunction( LPSTR lpBuffer, LPDWORD nSize);
        > >[/color]
        >
        >
        > [DllImport("your .dll", CallingConventi on = CallingConventi on.StdCall)]
        > static extern importfunction
        > (
        > [MarshalAs(Unman agedType.LPTStr )]
        > string lpBuffer,
        > ref int nSize
        > );
        >
        > Willy.
        >
        >
        >
        >[/color]

        Comment

        • Willy Denoyette [MVP]

          #5
          Re: import dll c++ function


          "centrino" <centrino@discu ssions.microsof t.com> wrote in message
          news:04E12B6F-66CE-4B87-BFC5-674E7EC80510@mi crosoft.com...[color=blue]
          >I don't know why i still get a empty buffer :
          >
          > there is a other similar function:
          >
          > DWORD WINAPI importfunction(
          > LPWSTR lpBuffer,
          > LPDWORD nSize
          > );
          >
          >
          > [DllImport("mydl l", SetLastError=tr ue)]
          > private static extern uint importfunction(
          >
          > [MarshalAs(Unman agedType.LPWStr )]
          > StringBuilder pBuffer,
          > ref uint pBufferSize
          > );
          >
          > "Willy Denoyette [MVP]" wrote:
          >[/color]

          Sorry but this is not the signature I've posted, WINAPI means stdcall
          calling convention, why did you change it.
          Also changed is the string argument, now it is a LPWSTR, why the change?

          Did you allocate a StringBuilder big enough to hold the returned string?
          What is there returned as nSize?
          The problem with PInvoke is that you really need the description of the
          function, else you have to guess things like:
          - LPWSTR is what a fixed size buffer or not, who allocates the buffer?
          - nSize is what the size of the buffer returned or the size requested?

          Willy.



          Comment

          • Willy Denoyette [MVP]

            #6
            Re: import dll c++ function


            "centrino" <centrino@discu ssions.microsof t.com> wrote in message
            news:B72B2C41-D57B-4D7D-BD42-F2AD0BFA5DBB@mi crosoft.com...[color=blue]
            > Hi Willy,
            >
            > Thanks for your post !
            >
            > I get the the size of the buffer returned nSize, but i "see" notthing in
            > StringBuffer lpBuffer.
            > StringBuilder lpBuffer = new StringBuilder(2 56);
            >
            > The function return ERROR_SUCCESS=0 and NDIS_ERROR_SUCC ESS=0. It seems
            > correct !?
            >
            > i dont know why ! I tryed to get buffer with byte[], but i get notthing.
            >
            > regards
            >
            >[/color]

            What's the Length of the SB reurned?
            Did yoy try lpBuffer.ToStri ng()?
            Are you sure the buffer contains a UNICODE string?
            What function are you calling exactly?

            Willy.


            Comment

            Working...