How do I pinvoke this function?

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

    How do I pinvoke this function?

    In a certain dll, there is this function:
    typedef DWORD STDAPICALLTYPE SOMEFUNC(VOID *pHandle, IProgress
    *pProgress);

    where IProgress is
    class IProgress { public: virtual void Update(UINT progress) = 0; };

    To pinvoke this from C#, I tried the following:
    [DllImport("THE_ DLL.dll", SetLastError = true)]
    static extern void SomeFunc(IntPtr handle, Progress progress);

    and defined Progress as
    public class Progress
    {
    public void Update(ushort progress)
    {
    // some code
    }
    }

    Trying to invoke SomeFunc and passing an object of Progress, I get an
    "Attempted to read or write protected memory" error. The other methods
    in the same dll that don't involve the progress callback object (but
    involve the handle parameter) work fine.

    Please let me know how to pinvoke this function.

  • Nicholas Paldino [.NET/C# MVP]

    #2
    Re: How do I pinvoke this function?

    Karthik,

    I believe in this case that IProgress is not actually a class, but an
    interface pointer. The thing is, while you will be able to call it in C#,
    you won't be able to call the methods on the interface pointer, as it needs
    to be a COM interface pointer in order to interop correctly. .NET interop
    does not support native C++ interfaces.

    Your P/Invoke declaration would look like this:

    [DllImport("THE_ DLL.dll", SetLastError = true)]
    static extern void SomeFunc(IntPtr handle, IntPtr progress);

    And the best you could do with the progress pointer is pass it around.


    --
    - Nicholas Paldino [.NET/C# MVP]
    - mvp@spam.guard. caspershouse.co m


    "Karthik V" <karthikveerama ni@gmail.comwro te in message
    news:1182443257 .074652.317580@ g37g2000prf.goo glegroups.com.. .
    In a certain dll, there is this function:
    typedef DWORD STDAPICALLTYPE SOMEFUNC(VOID *pHandle, IProgress
    *pProgress);
    >
    where IProgress is
    class IProgress { public: virtual void Update(UINT progress) = 0; };
    >
    To pinvoke this from C#, I tried the following:
    [DllImport("THE_ DLL.dll", SetLastError = true)]
    static extern void SomeFunc(IntPtr handle, Progress progress);
    >
    and defined Progress as
    public class Progress
    {
    public void Update(ushort progress)
    {
    // some code
    }
    }
    >
    Trying to invoke SomeFunc and passing an object of Progress, I get an
    "Attempted to read or write protected memory" error. The other methods
    in the same dll that don't involve the progress callback object (but
    involve the handle parameter) work fine.
    >
    Please let me know how to pinvoke this function.
    >

    Comment

    • Ben Voigt [C++ MVP]

      #3
      Re: How do I pinvoke this function?


      "Nicholas Paldino [.NET/C# MVP]" <mvp@spam.guard .caspershouse.c omwrote in
      message news:%23%232aRX CtHHA.4796@TK2M SFTNGP04.phx.gb l...
      Karthik,
      >
      I believe in this case that IProgress is not actually a class, but an
      interface pointer. The thing is, while you will be able to call it in C#,
      you won't be able to call the methods on the interface pointer, as it
      needs to be a COM interface pointer in order to interop correctly. .NET
      interop
      It looks like a COM interface pointer to me... or at least binary
      compatible. No IUnknown or IDispatch support though.

      You'll need a type library for it, and to run tlbimp.

      The lack of IUnknown may cause problems.
      does not support native C++ interfaces.
      >
      Your P/Invoke declaration would look like this:
      >
      [DllImport("THE_ DLL.dll", SetLastError = true)]
      static extern void SomeFunc(IntPtr handle, IntPtr progress);
      >
      And the best you could do with the progress pointer is pass it around.
      >
      >
      --
      - Nicholas Paldino [.NET/C# MVP]
      - mvp@spam.guard. caspershouse.co m
      >
      >
      "Karthik V" <karthikveerama ni@gmail.comwro te in message
      news:1182443257 .074652.317580@ g37g2000prf.goo glegroups.com.. .
      >In a certain dll, there is this function:
      >typedef DWORD STDAPICALLTYPE SOMEFUNC(VOID *pHandle, IProgress
      >*pProgress);
      >>
      >where IProgress is
      >class IProgress { public: virtual void Update(UINT progress) = 0; };
      >>
      >To pinvoke this from C#, I tried the following:
      > [DllImport("THE_ DLL.dll", SetLastError = true)]
      > static extern void SomeFunc(IntPtr handle, Progress progress);
      >>
      >and defined Progress as
      > public class Progress
      > {
      > public void Update(ushort progress)
      > {
      > // some code
      > }
      > }
      >>
      >Trying to invoke SomeFunc and passing an object of Progress, I get an
      >"Attempted to read or write protected memory" error. The other methods
      >in the same dll that don't involve the progress callback object (but
      >involve the handle parameter) work fine.
      >>
      >Please let me know how to pinvoke this function.
      >>
      >
      >

      Comment

      • Nicholas Paldino [.NET/C# MVP]

        #4
        Re: How do I pinvoke this function?

        That's the thing, if it doesn't derive from IUnknown, then it can't
        possibly be a COM interface pointer. Just because it is an interface
        doesn't mean that it is supported by COM.

        --
        - Nicholas Paldino [.NET/C# MVP]
        - mvp@spam.guard. caspershouse.co m

        "Ben Voigt [C++ MVP]" <rbv@nospam.nos pamwrote in message
        news:%23orbHrCt HHA.4796@TK2MSF TNGP04.phx.gbl. ..
        >
        "Nicholas Paldino [.NET/C# MVP]" <mvp@spam.guard .caspershouse.c omwrote
        in message news:%23%232aRX CtHHA.4796@TK2M SFTNGP04.phx.gb l...
        >Karthik,
        >>
        > I believe in this case that IProgress is not actually a class, but an
        >interface pointer. The thing is, while you will be able to call it in
        >C#, you won't be able to call the methods on the interface pointer, as it
        >needs to be a COM interface pointer in order to interop correctly. .NET
        >interop
        >
        It looks like a COM interface pointer to me... or at least binary
        compatible. No IUnknown or IDispatch support though.
        >
        You'll need a type library for it, and to run tlbimp.
        >
        The lack of IUnknown may cause problems.
        >
        >does not support native C++ interfaces.
        >>
        > Your P/Invoke declaration would look like this:
        >>
        >[DllImport("THE_ DLL.dll", SetLastError = true)]
        >static extern void SomeFunc(IntPtr handle, IntPtr progress);
        >>
        > And the best you could do with the progress pointer is pass it around.
        >>
        >>
        >--
        > - Nicholas Paldino [.NET/C# MVP]
        > - mvp@spam.guard. caspershouse.co m
        >>
        >>
        >"Karthik V" <karthikveerama ni@gmail.comwro te in message
        >news:118244325 7.074652.317580 @g37g2000prf.go oglegroups.com. ..
        >>In a certain dll, there is this function:
        >>typedef DWORD STDAPICALLTYPE SOMEFUNC(VOID *pHandle, IProgress
        >>*pProgress) ;
        >>>
        >>where IProgress is
        >>class IProgress { public: virtual void Update(UINT progress) = 0; };
        >>>
        >>To pinvoke this from C#, I tried the following:
        >> [DllImport("THE_ DLL.dll", SetLastError = true)]
        >> static extern void SomeFunc(IntPtr handle, Progress progress);
        >>>
        >>and defined Progress as
        >> public class Progress
        >> {
        >> public void Update(ushort progress)
        >> {
        >> // some code
        >> }
        >> }
        >>>
        >>Trying to invoke SomeFunc and passing an object of Progress, I get an
        >>"Attempted to read or write protected memory" error. The other methods
        >>in the same dll that don't involve the progress callback object (but
        >>involve the handle parameter) work fine.
        >>>
        >>Please let me know how to pinvoke this function.
        >>>
        >>
        >>
        >
        >

        Comment

        • Willy Denoyette [MVP]

          #5
          Re: How do I pinvoke this function?

          "Karthik V" <karthikveerama ni@gmail.comwro te in message
          news:1182443257 .074652.317580@ g37g2000prf.goo glegroups.com.. .
          In a certain dll, there is this function:
          typedef DWORD STDAPICALLTYPE SOMEFUNC(VOID *pHandle, IProgress
          *pProgress);
          >
          where IProgress is
          class IProgress { public: virtual void Update(UINT progress) = 0; };
          >
          To pinvoke this from C#, I tried the following:
          [DllImport("THE_ DLL.dll", SetLastError = true)]
          static extern void SomeFunc(IntPtr handle, Progress progress);
          >
          and defined Progress as
          public class Progress
          {
          public void Update(ushort progress)
          {
          // some code
          }
          }
          >
          Trying to invoke SomeFunc and passing an object of Progress, I get an
          "Attempted to read or write protected memory" error. The other methods
          in the same dll that don't involve the progress callback object (but
          involve the handle parameter) work fine.
          >
          Please let me know how to pinvoke this function.
          >

          It looks like IProgress is a native C++ class, you can't use such classes
          directly from C#, you will have to wrap this class in a managed wrapper
          using C++/CLI (VS2005 C++ using /clr option).

          Willy.

          Comment

          • Ben Voigt [C++ MVP]

            #6
            Re: How do I pinvoke this function?


            "Nicholas Paldino [.NET/C# MVP]" <mvp@spam.guard .caspershouse.c omwrote in
            message news:ePBhq1CtHH A.1212@TK2MSFTN GP05.phx.gbl...
            That's the thing, if it doesn't derive from IUnknown, then it can't
            possibly be a COM interface pointer. Just because it is an interface
            doesn't mean that it is supported by COM.
            COM is as much a binary standard for v-table layout as anything else, and
            that interface would meet the COM binary standard.
            >
            --
            - Nicholas Paldino [.NET/C# MVP]
            - mvp@spam.guard. caspershouse.co m
            >
            "Ben Voigt [C++ MVP]" <rbv@nospam.nos pamwrote in message
            news:%23orbHrCt HHA.4796@TK2MSF TNGP04.phx.gbl. ..
            >>
            >"Nicholas Paldino [.NET/C# MVP]" <mvp@spam.guard .caspershouse.c omwrote
            >in message news:%23%232aRX CtHHA.4796@TK2M SFTNGP04.phx.gb l...
            >>Karthik,
            >>>
            >> I believe in this case that IProgress is not actually a class, but an
            >>interface pointer. The thing is, while you will be able to call it in
            >>C#, you won't be able to call the methods on the interface pointer, as
            >>it needs to be a COM interface pointer in order to interop correctly.
            >>.NET interop
            >>
            >It looks like a COM interface pointer to me... or at least binary
            >compatible. No IUnknown or IDispatch support though.
            >>
            >You'll need a type library for it, and to run tlbimp.
            >>
            >The lack of IUnknown may cause problems.
            >>
            >>does not support native C++ interfaces.
            >>>
            >> Your P/Invoke declaration would look like this:
            >>>
            >>[DllImport("THE_ DLL.dll", SetLastError = true)]
            >>static extern void SomeFunc(IntPtr handle, IntPtr progress);
            >>>
            >> And the best you could do with the progress pointer is pass it
            >>around.
            >>>
            >>>
            >>--
            >> - Nicholas Paldino [.NET/C# MVP]
            >> - mvp@spam.guard. caspershouse.co m
            >>>
            >>>
            >>"Karthik V" <karthikveerama ni@gmail.comwro te in message
            >>news:11824432 57.074652.31758 0@g37g2000prf.g ooglegroups.com ...
            >>>In a certain dll, there is this function:
            >>>typedef DWORD STDAPICALLTYPE SOMEFUNC(VOID *pHandle, IProgress
            >>>*pProgress );
            >>>>
            >>>where IProgress is
            >>>class IProgress { public: virtual void Update(UINT progress) = 0; };
            >>>>
            >>>To pinvoke this from C#, I tried the following:
            >>> [DllImport("THE_ DLL.dll", SetLastError = true)]
            >>> static extern void SomeFunc(IntPtr handle, Progress progress);
            >>>>
            >>>and defined Progress as
            >>> public class Progress
            >>> {
            >>> public void Update(ushort progress)
            >>> {
            >>> // some code
            >>> }
            >>> }
            >>>>
            >>>Trying to invoke SomeFunc and passing an object of Progress, I get an
            >>>"Attempted to read or write protected memory" error. The other methods
            >>>in the same dll that don't involve the progress callback object (but
            >>>involve the handle parameter) work fine.
            >>>>
            >>>Please let me know how to pinvoke this function.
            >>>>
            >>>
            >>>
            >>
            >>
            >
            >

            Comment

            • Nicholas Paldino [.NET/C# MVP]

              #7
              Re: How do I pinvoke this function?

              Ben,

              While that is the case, COM demands that all interfaces derive from
              IUnknown. If an interface has to derive from that, then the vtable for that
              interface is always going to be populated with the entries for the methods
              in IUnknown in the first three slots, follwed by the methods for the
              interface itself. Since the interface here doesn't derive from IUnknown,
              its vtable is not going to have those entries.

              There is no way that this interface could be used in COM.

              --
              - Nicholas Paldino [.NET/C# MVP]
              - mvp@spam.guard. caspershouse.co m

              "Ben Voigt [C++ MVP]" <rbv@nospam.nos pamwrote in message
              news:%23qFCpDDt HHA.1060@TK2MSF TNGP06.phx.gbl. ..
              >
              "Nicholas Paldino [.NET/C# MVP]" <mvp@spam.guard .caspershouse.c omwrote
              in message news:ePBhq1CtHH A.1212@TK2MSFTN GP05.phx.gbl...
              > That's the thing, if it doesn't derive from IUnknown, then it can't
              >possibly be a COM interface pointer. Just because it is an interface
              >doesn't mean that it is supported by COM.
              >
              COM is as much a binary standard for v-table layout as anything else, and
              that interface would meet the COM binary standard.
              >
              >>
              >--
              > - Nicholas Paldino [.NET/C# MVP]
              > - mvp@spam.guard. caspershouse.co m
              >>
              >"Ben Voigt [C++ MVP]" <rbv@nospam.nos pamwrote in message
              >news:%23orbHrC tHHA.4796@TK2MS FTNGP04.phx.gbl ...
              >>>
              >>"Nicholas Paldino [.NET/C# MVP]" <mvp@spam.guard .caspershouse.c omwrote
              >>in message news:%23%232aRX CtHHA.4796@TK2M SFTNGP04.phx.gb l...
              >>>Karthik,
              >>>>
              >>> I believe in this case that IProgress is not actually a class, but
              >>>an interface pointer. The thing is, while you will be able to call it
              >>>in C#, you won't be able to call the methods on the interface pointer,
              >>>as it needs to be a COM interface pointer in order to interop
              >>>correctly. .NET interop
              >>>
              >>It looks like a COM interface pointer to me... or at least binary
              >>compatible. No IUnknown or IDispatch support though.
              >>>
              >>You'll need a type library for it, and to run tlbimp.
              >>>
              >>The lack of IUnknown may cause problems.
              >>>
              >>>does not support native C++ interfaces.
              >>>>
              >>> Your P/Invoke declaration would look like this:
              >>>>
              >>>[DllImport("THE_ DLL.dll", SetLastError = true)]
              >>>static extern void SomeFunc(IntPtr handle, IntPtr progress);
              >>>>
              >>> And the best you could do with the progress pointer is pass it
              >>>around.
              >>>>
              >>>>
              >>>--
              >>> - Nicholas Paldino [.NET/C# MVP]
              >>> - mvp@spam.guard. caspershouse.co m
              >>>>
              >>>>
              >>>"Karthik V" <karthikveerama ni@gmail.comwro te in message
              >>>news:1182443 257.074652.3175 80@g37g2000prf. googlegroups.co m...
              >>>>In a certain dll, there is this function:
              >>>>typedef DWORD STDAPICALLTYPE SOMEFUNC(VOID *pHandle, IProgress
              >>>>*pProgress) ;
              >>>>>
              >>>>where IProgress is
              >>>>class IProgress { public: virtual void Update(UINT progress) = 0; };
              >>>>>
              >>>>To pinvoke this from C#, I tried the following:
              >>>> [DllImport("THE_ DLL.dll", SetLastError = true)]
              >>>> static extern void SomeFunc(IntPtr handle, Progress progress);
              >>>>>
              >>>>and defined Progress as
              >>>> public class Progress
              >>>> {
              >>>> public void Update(ushort progress)
              >>>> {
              >>>> // some code
              >>>> }
              >>>> }
              >>>>>
              >>>>Trying to invoke SomeFunc and passing an object of Progress, I get an
              >>>>"Attempte d to read or write protected memory" error. The other methods
              >>>>in the same dll that don't involve the progress callback object (but
              >>>>involve the handle parameter) work fine.
              >>>>>
              >>>>Please let me know how to pinvoke this function.
              >>>>>
              >>>>
              >>>>
              >>>
              >>>
              >>
              >>
              >
              >

              Comment

              Working...