How do I correctly get the data pointed to by a reference/pointer (specifically pointer in my case)?
I've tried using empty square brackets [] as mentioned in the tutorial part 1 but just get an error relating to arrays. A further Google search turned up a news update here from 2012 mentioning the older deference operator ^ being removed but not replaced with anything.
Thanks, Brendon
If your pointer is a ptr T (for some T, like int for example), then p[] should work to dereference your pointer.
Is your pointer of type ptr T or type pointer? The latter (pointer) is analogous to void * in C, so you won't be able to dereference it.
Can you post a code snippet?
Thank you jboy, my problem is my use of pointer rather than ptr T.
As a mixed learning exercise/tool project (I do a lot of imaging for both Windows 7 and 8 at work) I was trying to port the VC++ code at http://sysadmins.no/?tag=msdm to Nim and was getting hung up on line 046 with FirmwareTableID = *pFirmwareTableID;
var b = cast[ptr int](voidPointer)[] # b is int stored at voidPointer addr
The dereference operator seems to work fine on built-in types, but how about on user-defined types? (In this case DWORD from the windows module)
With this usage: FirmwareTableID = pFirmwareTableID[]
I get this error: Error: type mismatch: got (typedesc[ptr DWORD])
but expected one of:
system.[](a: array[Idx, T], x: Slice[Idx]): seq[T]
system.[](s: string, x: Slice[int]): string
system.[](s: seq[T], x: Slice[int]): seq[T]
system.[](a: array[Idx, T], x: Slice[int]): seq[T]
Could you post a short snippet of the code you're trying to compile, including any type definitions and variable definitions that are being used in the snippet?
It's difficult to guess what the problem could be without any context.
Sure... http://pastebin.com/8BpwgMQd
I am by far not finished porting the original VC++ code yet (http://sysadmins.no/?tag=msdm) but have been test compiling as I go. After adjusting line 44 according to the error that throws, line 48 then gives the error I posted earlier.
Never mind my fail attempt at dealing with the C++ multicharacter literal assignment to the DWORD variable FirmwareTableProviderSignature on line 27 as I've already spent time with a hex editor and both C++ and separate Nim code and managed to duplicate the result of that assignment in Nim.
Thanks, Brendon
I suspect that the immediate source of the problem is line 24, pFirmwareTableID = ptr DWORD.
For context, this line appears in a variable declaration block:
var
FirmwareTableProviderSignature, BufferSize, FirmwareTableID: DWORD
vpFirmwareTableBuffer: PVOID
BytesWritten: DWORD
pFirmwareTableID = ptr DWORD # <-- this line
foundTable: bool = false
I suspect that you meant to type pFirmwareTableID: ptr DWORD (ie, using : for type declaration rather than = for assignment).
(In fact, I'm actually slightly amazed & confused that the compiler doesn't complain at you about var pFirmwareTableID = ptr DWORD. Similar code var x = ptr int32 doesn't compile for me.)
More generally speaking, those casts make me uncomfortable, but I can see in the windows module that PVOID is a type alias for pointer and DWORD is a type alias for int32, so I suppose you don't have much choice in the matter, if the original code casts a PVOID to a DWORD *.
What code do you use to convert a C++ multicharacter literal to a DWORD?
As jboy noted, in your code, you declare pFirmwareTableID as a type descriptor for ptr DWORD rather than as a variable of ptr DWORD.
Briefly, if you use:
var x = SomeType
instead of
var x: SomeType
Then you're declaring x as a variable that holds a type descriptor for SomeType rather than a value of type SomeType.
Note the colon vs the equals sign.
Thank you again jboy (and Jehan), fixing my variable declaration allowed the rest of that code to compile without changes.
For the C++ multicharacter literal I declared a (reversed because VC++ changes byte order) char array of ['I','P','C','A'] as well as an int32 or DWORD variable, got pointers to both, and then did a copyMem of the 4 bytes from the character array to the int32. I then checked the value of the int32/DWORD both from Nim and C++ and they were indeed the same.