Announcement

Collapse
No announcement yet.

C to PowerBasic translation

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • C to PowerBasic translation

    In C function a parameter is defined as follows:
    const void *

    How would one transloate this to PowerBasic?
    Apparantly, the parameter is used for passing a string.

    Thanks

  • #2
    I'd be inclined to declare it AS ANY and pass the specific address from the PB code with VARPTR/STRPTR, etc.

    However, there may be a more appropriate solution. Can you please post the whole prototype for that function?

    BTW, when you say "string", do you mean a 'char' array (ASCIIZ in PB) or a 'BSTR' (dynamic string in PB)?



    ------------------
    Lance
    PowerBASIC Support
    mailto:[email protected][email protected]</A>
    Lance
    mailto:[email protected]

    Comment


    • #3
      This is the original declaration in C:

      #define C_RET int _export _stdcall
      #define C_IN const
      #define C_PTR *
      typedef int CRYPT_HANDLE;

      and

      CRYPT_ATTRIBUTE_TYPE is an enum

      C_RET cryptSetAttributeString(C_IN CRYPT_HANDLE cryptHandle,
      C_IN CRYPT_ATTRIBUTE_TYPE attributeType,
      C_IN void C_PTR value, C_IN int valueLength );


      The help of the function tells this:
      int cryptSetAttributeString( const CRYPT_HANDLE cryptObject,
      const CRYPT_ATTRIBUTE_TYPE attributeType,
      const void *value, const int valueLength );

      Parameters
      cryptObject The object to which to add the value.
      attributeType The attribute which is being added.
      value The address of the data being added.
      valueLength The length in bytes of the data being added.

      Thanks


      Comment


      • #4
        Hi there

        since cryptlib is designed to work with different OSes definitely
        use an ASCIIZ string so your function define should look like

        Code:
        DECLARE FUNCTION cryptSetAttributeString LIB "libraryname.dll" _
        ALIAS "cryptSetAttributeString" ( BYVAL lcryptObject AS LONG, _
        BYVAL dwattributeType AS DWORD, BYVAL dwValue AS DWORD, _
        BYVAL lvalueLength AS LONG ) AS LONG
        and you would call the function something like:

        Code:
        LOCAL lReturn AS LONG
        LOCAL lHandle AS LONG
        LOCAL dwAttributes AS DWORD
        LOCAL szValue AS ASCIIZ * 1024 'or whatever length is appropriate
        
        lReturn = cryptSetAttributeString( lHandle, dwAttributes, VARPTR(szValue), _
                                            LEN(szValue) )
        Cheers

        Florent


        int cryptSetAttributeString( const CRYPT_HANDLE cryptObject,
        const CRYPT_ATTRIBUTE_TYPE attributeType,
        const void *value, const int valueLength );


        ------------------

        Comment


        • #5
          Thank you all for your replies.
          There is one more issue with passing the string as specified.

          The C routine in the DLL puts information in a PowerBasic Asciiz, but when I display that asciiz I am missing the first four characters that I would expect. And, at the end, there are four random characters added. It seems that the DLL-routine puts the information in memory 4 bytes to the left then I would expect.

          I pass the asciiz as varptr(asciiz), which seems correct to me.

          Does anyone understand what goes wrong here?

          Thanks

          Comment


          • #6
            Possibly, the function is not expecting an ASCIIZ pointer for parameter 3?

            I would guess that the fourth parameter should be the buffer size:
            SIZEOF(szValue) rather than LEN(szValue).


            ------------------
            Tom Hanlin
            PowerBASIC Staff

            Comment


            • #7
              Tom, you have catched me, and i bet the problem also!:-)

              hi,

              you can try

              DECLARE FUNCTION cryptSetAttributeString LIB "libraryname.dll" _
              ALIAS "cryptSetAttributeString" ( BYVAL lcryptObject AS LONG, _
              BYVAL dwattributeType AS DWORD, BYREF dwValue AS ASCIIZ, _
              BYVAL lvalueLength AS LONG ) AS LONG

              lReturn = cryptSetAttributeString( lHandle, dwAttributes, szValue, _
              LEN(szValue) )


              you should also keep in mind, that the dll may expect a pointer
              to a buffer wich needs to be reallocated by the dll. So if you are not certain, what the type of buffer the dll want, you should use
              any of the "Memory Allocation" functions to create a buffer and
              pass the pointer to the buffer to the dll.

              Do you check the return value of the call? There may be a hint about what may have gone wrong.

              Regards


              ------------------


              [This message has been edited by Torsten Rienow (edited January 25, 2002).]

              Comment


              • #8
                For the record: Now that I'm passing a ASCIIZ PTR it works well:

                DECLARE FUNCTION cryptPopData LIB "CL32.DLL" _
                ALIAS "cryptPopData"(BYVAL envelope AS LONG, _
                Value AS ASCIIZ PTR, BYVAL ValueLength AS LONG, Copied AS LONG) AS LONG

                Thank you all.

                Comment

                Working...
                X