Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re^2: How to use 'unsigned char' C data type with Win32::API

by sam_bakki (Pilgrim)
on Mar 13, 2012 at 05:25 UTC ( #959236=note: print w/ replies, xml ) Need Help??


in reply to Re: How to use 'unsigned char' C data type with Win32::API
in thread Solved: How to use 'unsigned char' C data type with Win32::API

Hi BrowserUk

For some reason, having 'int' and getting data from my DLL is not working well.

my $i2cGetDeviceAddressFunc = Win32::API->new('CrdI2C32', 'int i2cGetD +eviceAddress()') or warn "\n ERROR: Can not import API:i2cGetDeviceAd +dress , $^E ,"; my $ret = 0; $ret = $i2cGetDeviceAddressFunc->Call(); print "\n RET: $ret";

Output

1. Run RET: 53113496 2. Run RET: 52195992 3. Run RET: 52851352 4. Run RET: 52458136 5. Run RET: 51540632

Output is not what i expected and more over every run, it produce different output. But the C code which calls the i2cGetDeviceAddress() API produces same output always.

I don't have source code of the DLL (CRDI2C32.DLL) , We bought this from other company, They gave C, C++, VB & PASCAL example code which has API prototypes.

PASCAL Sample: xx.pas Function i2cGetDeviceAddress : Byte; + stdcall; external 'CrdI2C32.DLL'; C Sample: xx.h typedef unsigned char __stdcall i2cGetDeviceAddress_type(); extern i2cGetDeviceAddress_type *i2cGetDeviceAddress; xx.c i2cGetDeviceAddress_type *i2cGetDeviceAddress = NULL; .... i2cGetDeviceAddress = (i2cGetDeviceAddress_type*)GetProcAddress(hDll, +"i2cGetDeviceAddress"); if (i2cGetDeviceAddress == NULL) { FreeLibrary(hDll); return 22; // function not found in library } VB Sample: xx.bas Declare Function i2cGetDeviceAddress Lib "CrdI2C32.DLL" () As Byte


Comment on Re^2: How to use 'unsigned char' C data type with Win32::API
Select or Download Code
Re^3: How to use 'unsigned char' C data type with Win32::API (&255)
by tye (Cardinal) on Mar 13, 2012 at 05:59 UTC

    I didn't look in detail (I suspect the relevant details could be buried quite deep), but it looks likely to be a bug in Win32::API to me.

    Even if not a bug in Win32::API, it looks like an uninitialized four-byte value is having only some of its bytes overwritten. So why not just work around it and pull out only the byte you care about?

    $ret &= 0xFF;

    Though, the return values you show actually show the lowest two bytes being consistently 0x7298, which seems rather unlikely.

    printf "%d 0x%x\n", $_, $_ for 53113496, 52195992, 52851352, 52458136, 51540632 53113496 0x32a7298 52195992 0x31c7298 52851352 0x3267298 52458136 0x3207298 51540632 0x3127298

    ...So it could easily be neither of the things I guessed. Is 0x98 (152) the value you are expecting to get?

    - tye        

      Hi tye

      I suppose to get like below C code's API call.

      # C Code # printf ("\n \n DEV: %d , %c , %x ", i2cGetDeviceAddress(),i2cGetDevi +ceAddress(),i2cGetDeviceAddress()); # # Output: DEV: 70 , F , 46

      I used following code get rid of un initialized bytes

      $ret = unpack ('C',pack ('i*',$ret)); #This gives 152 but this is not +i want :(

      Hi tye

      I suppose to get like below C code's API call.

      # C Code # printf ("\n \n DEV: %d , %c , %x ", i2cGetDeviceAddress(),i2cGetDevi +ceAddress(),i2cGetDeviceAddress()); # # Output: DEV: 70 , F , 46

      I used following code get rid of un initialized bytes

      $ret = unpack ('C',pack ('i*',$ret)); #This gives 152 but this is not +i want :(
Re^3: How to use 'unsigned char' C data type with Win32::API
by BrowserUk (Pope) on Mar 13, 2012 at 11:37 UTC
    We bought this from other company

    The one you have seems to be broken. What size is it? You probably need to send it back under warranty and exchange it for a new one.


    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.

    The start of some sanity?

      the same api working well in c code so I am sure dll is not broken. some how not working with perl i am missing something

        Have you seen this?

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://959236]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (15)
As of 2014-10-02 16:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    What is your favourite meta-syntactic variable name?














    Results (65 votes), past polls