Solved

Calling C Type DLL from VB.net to return Char() or String (passed as intptr)

Posted on 2004-04-06
3
798 Views
Last Modified: 2011-10-03
How do I retrieve a string / char() value from a dll which only accepts IntPtr(at least all my other attempts failed with eroors) as Variable.
I have a perfectly fine working c/c++ code to call the c-type dll which goes as follows (Hope I include all the relevant parts):

typedef KPDCStatus (*KPDCGetAttributeInfoFunc)(
                  KPDCOpaqueRef inRef,
                  KPDCAttributeID inAttrID,
                  KPDCDataType *outAttrType,
                  KPDCUInt32 *outAttrSize);

typedef KPDCStatus (*KPDCGetAttributeValueFunc)(                                        
                  KPDCOpaqueRef inRef,                                    
                  KPDCAttributeID inAttrID,
                  KPDCUInt32 *inOutAttrSize,
                  void* outAttrValue);

//The functions are loaded into the theProcs-Structure

char theMemoryPath[255];

theStatus = theProcs.GetAttributeInfo(theCamera, KPDCSerialNumberStringID,&theDataType, &theAttributeSize );

// Now get actual serial number and store it in the memory path
memset(theMemoryPath, 0, sizeof(theMemoryPath) );
theStatus = theProcs.GetAttributeValue( theCamera, KPDCSerialNumberStringID,&theAttributeSize, theMemoryPath);

The function returns : P14N-15076 (That's what I want)


Trying to transfer this call to VB.net my current version looks like this (aftertaking many forms in the meantime, trying some MarshalAs and whatever I read about the topic):

    <DllImport("DCSPro4SLR.dll")> Shared Function KPDCGetAttributeInfo(ByVal inRef As IntPtr, _
               ByVal inAttrID As UInt32, ByRef outAttrType As UInt32, ByRef outAttrSize As UInt32) As Integer
    End Function

    <DllImport("DCSPro4SLR.dll")> Shared Function KPDCGetAttributeValue(ByVal inRef As IntPtr, _
               ByVal inAttrID As UInt32, ByRef inOutAttrSize As UInt32, ByRef outAttrValue As IntPtr) As Integer
    End Function
'The KPDCGetAttributeValue Function is the one troubling me

Private sub Test
            theStatus = KPDCGetAttributeInfo(theCamera, Convert.ToUInt32(KPDCCamAttr.KPDCSerialNumberStringID), _
                    theDataType, theAttributeSize)

            Dim tmpMemPath As IntPtr = Nothing
            theMemoryPath = ""

            theStatus = KPDCGetAttributeValue(theCamera, Convert.ToUInt32(KPDCCamAttr.KPDCSerialNumberStringID), _
                      theAttributeSize, tmpMemPath)
end sub

Everything seems to work fine. The returned status is OK, similar to the c call, theAttributeSize changes from 11 to 10, but I can't find a way to get the string value based on the tmpMemPath = 1312043344 value returned by the function.
E.g. trying:
      theMemoryPath = Marshal.PtrToStringAuto(tmpMemPath)                  
 yields "" while all the other versions of Marhal.PtrToString...(tmpMemPath [,]) result in an reference not set to an object exception.
Just ask if additional information is needed.
0
Comment
Question by:Der_Steppenwolf
  • 2
3 Comments
 
LVL 48

Accepted Solution

by:
AlexFM earned 200 total points
ID: 10772031
In C code you allocate memory for output outAttrValue parameter:

char theMemoryPath[255];
memset(theMemoryPath, 0, sizeof(theMemoryPath) );
theStatus = theProcs.GetAttributeValue( theCamera, KPDCSerialNumberStringID,&theAttributeSize, theMemoryPath);

In VB you just pass NULL-pointer:
Dim tmpMemPath As IntPtr = Nothing
theStatus = KPDCGetAttributeValue(theCamera, Convert.ToUInt32(KPDCCamAttr.KPDCSerialNumberStringID), _
                      theAttributeSize, tmpMemPath)

Allocate memory by the same way as in C:

Dim tmpMemPath As IntPtr
tmpMemPath = Marshal.AllocHGlobal(255)
theStatus = KPDCGetAttributeValue(theCamera, Convert.ToUInt32(KPDCCamAttr.KPDCSerialNumberStringID), _
                      theAttributeSize, tmpMemPath)

Now try to extract valur written to tmpMemPath by KPDCGetAttributeValue.

0
 

Author Comment

by:Der_Steppenwolf
ID: 10777544
Thanxs for the quick help, one additional twist was, that the outAttrValue has to be passed as ByVal rather than ByRef. Based on your answer I finally grabbed the concept of passing pointers to dll :-)
0
 
LVL 48

Expert Comment

by:AlexFM
ID: 10780776
Right. I didn't see this.

ByVal outAttrValue As IntPtr        void* outAttrValue        
ByRef outAttrValue As IntPtr        void** outAttrValue
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A basic question.. “What is the Garbage Collector?” The usual answer given back: “Garbage collector is a background thread run by the CLR for freeing up the memory space used by the objects which are no longer used by the program.” I wondered …
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

770 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