Announcement

Collapse
No announcement yet.

Strings & C++ DLL

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

  • Strings & C++ DLL

    I'm working with a C++ DLL and one of the parameters in the
    function I'm calling requires a parameter of Class 'CString'.
    I've tried every dirty PB string passing trick I can think of to
    no avail (as I expected). Has anybody had to deal with a situation
    like this and have a workaround for it?



    ------------------
    Nick
    Nick

  • #2
    Although I have never had to convert a CString to PB, I'd like to know the answer too! Any chance you could post the declaration?

    Regards,

    ------------------
    email
    http://www.kgpsoftware.com
    kgpsoftware.com | Slam DBMS | PrpT Control | Other Downloads | Contact Me

    Comment


    • #3
      >I've tried every dirty PB string passing trick I can think of...

      Of which ones have you thought?

      Did you try passing ..
      - the address of a handle to a dynamic string?
      - the value of that handle by value?
      - the address of the string data when the string is null-terminated?
      - The address of NTS data BY REFERENCE and testing if the function changed it?

      And how does the call fail? You're not passing BYVAL or BYCOPY and expecting the function to return that string modified, are you?

      MCM


      Michael Mattias
      Tal Systems (retired)
      Port Washington WI USA
      [email protected]
      http://www.talsystems.com

      Comment


      • #4
        My C++ guru tells me that CString is an asciiz termintated string, so you should be able to do:

        call Foobar(a$ + chr$(0))

        Russ

        ------------------
        "There are two novels that can change a bookish fourteen-year old's life: The Lord of the Rings and Atlas Shrugged. One is a childish fantasy that often engenders a lifelong obsession with its unbelievable heroes, leading to an emotionally stunted, socially crippled adulthood, unable to deal with the real world. The other, of course, involves orcs." - John Rogers

        Comment


        • #5
          My guess is that you would pass an asciiz string pointer byval.

          DIM txt as ASCIIZ * 128 'whatever size is needed
          call routine(BYVAL VARPTR(txt))

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

          Comment


          • #6

            > My guess is that you would pass an asciiz string pointer byval.

            Sounds likely. A CString is a pointer to a character array.
            Pointers in C are passed by the value of the pointer.
            The asciiz string pointer in the called function will be given
            the value passed to it. It used to be a long integer value,
            then an unsigned long integer, and I'm not sure what any given
            C++ implementation might use as a pointer value.
            It is the integer value of the address space, though.
            I'm fairly certain of that.

            ]<urt


            ------------------
            The world is strange and wonderful.*
            I reserve the right to be horrifically wrong.
            Please maintain a safe following distance.
            *wonderful sold separately.

            Comment


            • #7
              Sorry I should have been more clear, you are passing a pointer to the asciiz string. If it's declared:

              Code:
              declare function Foobar(byref mystring as asciiz) Lib "SomeDll.dll" alias "Foobar"(byref mystring as asciiz) as long
                        or 
              declare Sub Foobar(byref mystring as asciiz) Lib "SomeDll.dll" alias "Foobar"(byref mystring as asciiz)
              the later in C/C++ is called a void because it doesn't return anything or it returns a pointer to a zen like state or something like that.

              Russ


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


              [This message has been edited by Russ Srole (edited February 05, 2005).]
              "There are two novels that can change a bookish fourteen-year old's life: The Lord of the Rings and Atlas Shrugged. One is a childish fantasy that often engenders a lifelong obsession with its unbelievable heroes, leading to an emotionally stunted, socially crippled adulthood, unable to deal with the real world. The other, of course, involves orcs." - John Rogers

              Comment


              • #8
                Thanks for the replies. I took the time to gather up more info and did some more
                research. Here's the results:

                ' This is how the C++ code calls the function (sPath has been declared as CString):
                error = m_BoardControl.BootARM(&sPath);

                ' This is how the function is declared in the MAP file:
                [email protected]@@[email protected]@[email protected]@@Z 004197d4 ttdvbacc:ttdvbacc.dll

                ' This is how I declare it in PB:
                DECLARE FUNCTION BootARM CDECL LIB "ttdvbacc.dll" ALIAS "[email protected]@@[email protected]@[email protected]@@Z" (sPath AS DWORD) AS LONG

                The function expects a NTS specifying the path to an ARM processor boot code file and
                returns an error code.

                The only call that doesn't generate a GPF (but returns a non-zero error code) is
                BootARM(VARPTR(sPath)) with sPath declared as STRING. I believe this parameter will
                look like a BSTR to the C code. The GPF's occur in "MFC42.DLL"

                The definition of CString can be found in the C++ header file "Afx.h".
                A structure is defined there that looks like this:

                struct CStringData
                {
                long nRefs; // reference count
                int nDataLength; // length of data (including terminator)
                int nAllocLength; // length of allocation
                // TCHAR data[nAllocLength]

                TCHAR* data() // TCHAR* to managed data
                { return (TCHAR*)(this+1); }
                };

                I'm thinking that the above is what needs to be passed as a parameter.
                This is a little more involved than what VARPTR(sString) provides - (pointer to string data and Length).
                I'm now unable to figure out how to manage this structure since it seems to also behave like a function
                (as it seems to return a pointer).

                Anyone have some thoughts on any of this?



                ------------------
                Nick
                Nick

                Comment


                • #9
                  Do you have the header file for the DLL in question? Are you
                  sure about the CDECL or just assuming? Most Windows C++ functions
                  are more commonly mapped to STDCALL, most of the time. Makes for
                  easier interfacing with different languages.

                  The C++ code is passing the "address of" sPath which should
                  translate to BYVAL of a ascii string pointer in PB.

                  Wow, C++ mangling (aka decaration) is the pits. The "m_" normally
                  indicates a class/member function. Trying to figure out how to call a C++
                  class function from PB seems to be the issue at hand.

                  I'll do some more checking.

                  Cheers

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

                  Comment


                  • #10
                    Sounds like class string.
                    If the ref counter becomes 0 c will most likely destroy this one.
                    OK, that's not your concern.
                    Concern is the char being passed, then use ASCIZ as mentioned above.

                    IF the c declare RETURNS a pointer to a cString then you should be careful and use a pointer with a similar construct.
                    If this is passed as parameter you can ecplore it's first copntents to know if the counter is passed in the first bytes.
                    If you see bad chars you can rewrite to use this type as well with PB.


                    ------------------
                    http://www.hellobasic.com
                    PwrDev - Visual Designer for pb/dll, pb/win, pb/cc
                    PwrStat - Static library linker for pb/dll or pb/win.
                    hellobasic

                    Comment


                    • #11
                      Something like:

                      Type CStringData

                      nRefs as long '; // reference count
                      nDataLength As Long ' ; // length of data (including terminator)
                      nAllocLength as int '; // length of allocation
                      pszText as Asciiz Ptr
                      End Type

                      DECLARE FUNCTION BootARM CDECL LIB "ttdvbacc.dll" ALIAS "[email protected]@@[email protected]@[email protected]@@Z" (sPath AS CSTRING) AS LONG

                      Local cs as CString
                      Local T AS String
                      T = String$( 100, 0 )

                      cs.nRefs = 1
                      cs.nDataLength = Len( T ) + 1
                      cs.nAllocLength = Len( T )
                      cs.pszText = StrPtr( T )

                      Pass to call now like:
                      BootARM( cs )

                      ------------------
                      http://www.hellobasic.com
                      PwrDev - Visual Designer for pb/dll, pb/win, pb/cc
                      PwrStat - Static library linker for pb/dll or pb/win.

                      [This message has been edited by Edwin Knoppert (edited February 07, 2005).]
                      hellobasic

                      Comment


                      • #12
                        Well, after some searching on the animal CString, it turns out
                        that it's basically a plain old string. It's intent was a better
                        interface to languages like Basic, Pascal, VB etc.

                        No, it's not an ASCII type string, though it can be converted to
                        one by simple type casting, ie (LPCTSTR). It can change in lenght
                        up to INT_MAX value, [2,147,483,647 Maximum (signed) int value].

                        The class has a myriad of member functions that perform various
                        operations on the string.

                        99.44 percent of the time, C++ uses a function pointer to call
                        member functions. You might give calling the "ordinal value"
                        found by using Dumpbin, use a standard PB string and pass it
                        as BYREF, which is the default call.

                        Good luck, hope this helps in some small way.

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

                        Comment


                        • #13
                          >plain old string...
                          What is that in your eyes?
                          In PB string means something different than usually used with c.


                          ------------------
                          http://www.hellobasic.com
                          PwrDev - Visual Designer for pb/dll, pb/win, pb/cc
                          PwrStat - Static library linker for pb/dll or pb/win.
                          hellobasic

                          Comment


                          • #14
                            A CString is not a data type. The data type is an asciiz string.
                            CString is a class which allows easier handling of strings.
                            It allows concatenation, allocation, and assignment of an
                            asciiz string using overloaded operators such as '+' and '='.
                            I have actually written such a class myself for a C++ exercise
                            under Borland 3.1. The purpose of CString is to allow the program
                            developer the ease and simplicity of BASIC style string handling,
                            which is greatly superior to C in ease of use. Using strings in
                            C requires close attention to detail. That's why we get those
                            informative warnings about buffer overruns in message headers and
                            .jpg headers from Microsoft every now and then. Strings.
                            I could quote from my VC++ online manual, but I don't know if it is
                            covered by fair usage in this instance.

                            ]<urt


                            ------------------
                            The world is strange and wonderful.*
                            I reserve the right to be horrifically wrong.
                            Please maintain a safe following distance.
                            *wonderful sold separately.

                            Comment


                            • #15
                              Never said the CString was a data type. Yes, it is a class in
                              C++. NO, it not an ASCII type string normally associated with
                              C/C++, though it can be changed to one very easily. Why you would
                              want to do that would be beyond me.

                              Cheers

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

                              Comment


                              • #16
                                Hi Cecil,

                                I need a help for a short project from someone with expertise in C/C++ and PB.
                                If you are available please respond to me through your private e-mail.
                                My e-mail address is: [email protected]

                                Regards,
                                joshua

                                Comment

                                Working...
                                X