PInvoke

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • grndvl1
    New Member
    • Dec 2009
    • 5

    PInvoke

    Here is my issue, I am very new to C# and want to invoke a custom dll that I can't register using regsvr32. I figured out I need to use PInvoke somehow but need help since all the methods return void, the data is in the Msg pointer. Some of the functions will need to send data using Msg and then will receive back in a modified pointer.

    Here is one example the message is a structure that is passed to GetTMVersionInf o ( TM_Message* Msg ) and by sending a blank message it will modify the TM_Message pointer with the real data. The dll is for a keyboard I need to write drivers to, a Bosch Intuikey.


    typedef struct
    {
    BYTE Msg[MAX_MSG_LEN];
    int MsgLen;
    } TM_Message;

    TM_Message will be used as an output parameter for all API’s. All API’s will return the formed packet in
    TM_Message structure.
    • Msg will be the framed packet.
    • MsgLen will be the length of packet.
    • MAX_MSG_LEN is a macro, having value 80.


    void GetTMVersionInf o ( TM_Message* Msg )
    This API will be used to form packet for information message “Get_Version”. Formed packet will return in output parameter TM_Message.
    Input Parameters: None
    Return Type: Void

    remember I am new to C# and not good at C, been a while so type slowly so I can understand :-)

    Thanks
    John
  • Dheeraj Joshi
    Recognized Expert Top Contributor
    • Jul 2009
    • 1129

    #2
    Code:
    void GetTMVersionInfo ( TM_Message* Msg )
    Are you changing the value of *Msg in this function.?

    Regards
    Dheeraj Joshi

    Comment

    • grndvl1
      New Member
      • Dec 2009
      • 5

      #3
      Actually i found the answer here it is

      [StructLayout(La youtKind.Sequen tial)]
      public struct TM_Message {
      [MarshalAs(Unman agedType.ByValA rray, SizeConst = ConstantsUsed.M AX_MSG_LEN)]
      public byte[] data;
      public int MsgLen;
      };

      public static class ConstantsUsed {
      public const int MAX_MSG_LEN = 80;
      }

      and then make the dll calls

      [DllImport("term inalModeDll.dll ")]
      private static extern void GetTMVersionInf o(ref TM_Message msg);

      and then make a generic method wrapper to do any processing:

      public TM_Message m_GetTMVersionI nfo(TM_Message message) {
      GetTMVersionInf o(ref message);
      return message;
      }

      Comment

      Working...