Getting and Object from a handle passed from a System DLL

Posted on 2005-04-28
Last Modified: 2012-06-27

I'm trying to interfae with the PStore dll in VB.NET.  Adding it as a reference works except, it doesn't make the PStoreCreateInstance function available.  You need to call this to get an instance of the PStore object.  Using "new" on the object class doesn't seen work, so I'm assuming this function still needs to be called.

It has the following interface in C:
HRESULT PStoreCreateInstance( IPStore** ppProvider,
  void* pReserved,  DWORD dwFlags);

So, I've defined the function as follows:
Private Declare Function PStoreCreateInstance Lib "pstorec.dll" (ByRef hPS As IntPtr, ByVal l1 As Long, ByVal l2 As Long, ByVal l3 As Long) As Integer (Only the first argument is necessary here.)
This works.  I can see  hPS has a value in the debugger, and the memory at that address looks like it could be the structure I need.  However, I now need to get this memory location to represent an instance of CPStoreClass to use it.

Maybe I'm going about this the wrong way.

What is the right way to get this object back from this function?

Question by:sllynn2020
    LVL 48

    Accepted Solution

    IPStore* pointer filled by PStoreCreateInstance function is pointer to interface. You need to call interface functions like CreateType, CreateSubtype etc. What is CPStoreClass that you expect to be pointed by IPStore?
    I have never worked with these functions, but maybe I can give some direction. For example, if you know memory structure pointed by IPStore*, you can use Marshal class functions to extract information from it.
    I beleive it is better to do in unmanaged C++ code, but possibly there ib VB .NET solution.
    LVL 14

    Expert Comment

    I wouldl try the DirectCast function. It has the following syntax,

    Dim Pstore As IPStore = DirectCast(hPS, IPStore)

    Author Comment


    Thank you both for your suggestions.

    AlexFM, To call CreateType, CreateSubtype, you need an instance of IPStore (actually the CPStoreClass implements the IPStore interface thus needing and instance of CPStoreClass).  To do this, you need to call PStoreCreateInstance.   If you call New, you get an Object, but not a CPStoreClass object since none of the classes have New functions.

    PTakja, I tried your suggestion, but all it did was give me an error about DirectCast needing a reference but hPS being a value.

    I've actually solved this problem by writing a simple standard Windows DLL that does all the work that I would have done to create the types as needed and the item in the PStore. (AlexFM's suggestion.)


    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

    Introduction When many people think of the WebBrowser ( control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
    It’s quite interesting for me as I worked with Excel using for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
    It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
    Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

    745 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now