How to use COM in php?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Darek

    How to use COM in php?

    Hello,

    I have just created new my own activeX, named "arg.ocx", using Visual C++ 6.
    I registered it successully, using regsvr32.exe
    How can I use this arg.ocx, in php script?

    I have been trying in this way:
    ----
    $mycom = new COM("arch.CArch Ctrl); // OK
    $tekst = $mycom->myhellofunctio n("Piotr"); // ERROR
    print($tekst);
    $mycom = null;
    ----

    Unfortunately, I got the following error message:
    Warning: (null)(): Invalid ProgID, GUID string, or Moniker: Incorrect syntax

    How should I fix it?
    In advance thanks a lot!

    --
    best regards,
    Darek

    PS.
    In Visual C++, I used project "MFC ActiveX ControlWizard" and I added one
    method in class CArchCtrl, in the following way:

    char* CArchCtrl::myhe llofunction(cha r* c)
    {
    char* tekst = "Hello ";
    strcat(tekst, c);
    return tekst;
    }


  • Chung Leong

    #2
    Re: How to use COM in php?

    "Darek" <interdarekb@NO SPAMpoczta.onet .pl> wrote in message
    news:ccb0c7$k3u $1@nemesis.news .tpi.pl...[color=blue]
    > In Visual C++, I used project "MFC ActiveX ControlWizard" and I added one
    > method in class CArchCtrl, in the following way:
    >
    > char* CArchCtrl::myhe llofunction(cha r* c)
    > {
    > char* tekst = "Hello ";
    > strcat(tekst, c);
    > return tekst;
    > }
    >[/color]

    I'm rather rusty with COM, but that doesn't look like a dispatchable
    function to me. Shouldn't it be HRESULT CArchCtrl::myhe llfunction(in
    olevariant *c, out olevariant **r) or something like that?

    Use the ActiveX test container to see what methods/properties are available.

    --
    Obey the Clown - http://www.conradish.net/bobo/


    Comment

    • Harry

      #3
      Re: How to use COM in php?

      "Darek" <interdarekb@NO SPAMpoczta.onet .pl> wrote in message news:<ccb0c7$k3 u$1@nemesis.new s.tpi.pl>...[color=blue]
      > ----
      > $mycom = new COM("arch.CArch Ctrl); // OK
      > $tekst = $mycom->myhellofunctio n("Piotr"); // ERROR
      > print($tekst);
      > $mycom = null;
      > ----[/color]


      Could it be because of the missing "
      $mycom = new COM("arch.CArch Ctrl);
      should be?
      $mycom = new COM("arch.CArch Ctrl");

      Comment

      • Darek

        #4
        Re: How to use COM in php?


        User "Harry" <harryman100@ho tmail.com> wrote:
        [color=blue]
        > $mycom = new COM("arch.CArch Ctrl");[/color]

        --
        Yes. I had this, This line works ok.
        I STILL couldn't invoke method from COM object using PHP. Is it impossible?
        Please help.
        What can I do?

        regards,
        Darek


        Comment

        • emerth

          #5
          Re: How to use COM in php?

          "Darek" <interdarekb@NO SPAMpoczta.onet .pl> wrote in message news:<ccb0c7$k3 u$1@nemesis.new s.tpi.pl>...[color=blue]
          > Hello,
          >
          > I have just created new my own activeX, named "arg.ocx", using Visual C++ 6.[/color]

          <snip>
          [color=blue]
          >
          > PS.
          > In Visual C++, I used project "MFC ActiveX ControlWizard" and I added one
          > method in class CArchCtrl, in the following way:
          >
          > char* CArchCtrl::myhe llofunction(cha r* c)
          > {
          > char* tekst = "Hello ";
          > strcat(tekst, c);[/color]
          ^^^^^^^^^^^^^^^ ^^
          This is illegal in C++ for a few reasons...[color=blue]
          > return tekst;
          > }[/color]

          1) You have declared a static char string. ANSI says such are not
          modifiable. In practice some compilers let you do it anyway.
          2) A C/C++ "char string" is not a "string" in the scripting language
          sense of the word. What it is, is a fixed length array of characters.
          Such does not re-size itself when you append to it. Writing beyond the
          end of the array is an incorrect and 99.9999999% of the time will
          cause a crash. COM will not nesc. generate an error message implying
          such a C++ error.

          If you want to do what your code suggests, you want something like
          this:

          // let tekst be a member variable of the class, declared as such:
          class CArchCtrl {
          protected:
          char tekst[64];
          [ rest of your other class declaration]
          }

          char* CArchCtrl::myhe llofunction(cha r* c) {
          memset(tekst, '\0', 64); // make the array contents all null
          character.
          strcpy(tekst, "Hello "); // Set beginning of string to "Hello "
          strcat(tekst, c); // Append the char string pointed to by c
          return tekst;
          }

          But even here, consider: if c points to a char string longer than 57
          chars, you overrun tekst and crash. C/C++ don't forgive much. You
          could use strncat() with 3rd arg being 63 - strlen("Hello ")

          Have fun,
          Eric

          Comment

          • emerth

            #6
            Re: How to use COM in php?

            "Darek" <interdarekb@NO SPAMpoczta.onet .pl> wrote in message news:<ccb0c7$k3 u$1@nemesis.new s.tpi.pl>...[color=blue]
            > Hello,
            >
            > I have just created new my own activeX, named "arg.ocx", using Visual C++ 6.[/color]

            ....
            [color=blue]
            > In Visual C++, I used project "MFC ActiveX ControlWizard" and I added one
            > method in class CArchCtrl, in the following way:
            >
            > char* CArchCtrl::myhe llofunction(cha r* c)
            > {
            > char* tekst = "Hello ";
            > strcat(tekst, c);
            > return tekst;
            > }[/color]

            And, I may as well point out that returning a pointer to storage
            allocated on the stack is a Bad Idea. The memory tekst points to go
            out of scope when the function returns. Your returned pointer will not
            point to valid data, which will crash your code when you later try to
            use the data.

            Allocate the space for your char string either:
            - using new
            - or, make it a class member vavriable
            - or, have the calling code provide a pointer to space that it manages

            Comment

            • Default User

              #7
              Re: How to use COM in php?

              emerth wrote:
              [color=blue][color=green]
              > > char* CArchCtrl::myhe llofunction(cha r* c)
              > > {
              > > char* tekst = "Hello ";
              > > strcat(tekst, c);
              > > return tekst;
              > > }[/color]
              >
              > And, I may as well point out that returning a pointer to storage
              > allocated on the stack is a Bad Idea.[/color]

              That's true.
              [color=blue]
              > The memory tekst points to go
              > out of scope when the function returns.[/color]

              That's wrong. tekst is initialized to a string literal, which is not
              automatic storage. String literals have static duration, they exist for
              the life of the program. The pointer itself is in automatic storage, but
              a copy of it is returned, so no problem.

              This would be a problem:

              char* CArchCtrl::myhe llofunction(cha r* c)
              {
              char tekst[] = "Hello ";
              return tekst;
              }

              In that case, tekst would be an array allocated in automatic storage. It
              would go out of scope after the return from the function and any use
              would exhibit undefined behavior.
              [color=blue]
              > Your returned pointer will not
              > point to valid data, which will crash your code when you later try to
              > use the data.[/color]

              There is no guarantee that undefined behavior will cause a crash.
              Another possibility is that it will seem to run fine until you make some
              seemingly unrelated change, then crash. Or it may just overwrite other
              automatic data, silently changing values. Don't do it, and don't count
              the compiler or the run-time environment catching it.
              [color=blue]
              > Allocate the space for your char string either:
              > - using new[/color]

              Ok.
              [color=blue]
              > - or, make it a class member vavriable[/color]

              How does that allocate space?
              [color=blue]
              > - or, have the calling code provide a pointer to space that it manages[/color]

              The best solution is to use the std::string library container.

              #include <string>

              std::string CArchCtrl::myhe llofunction(cha r* c)
              {
              std::string tekst = "Hello ";
              tekst += c;
              return tekst;
              }

              To avoid copying the string, then a reference to a string could be
              passed int.



              Brian Rodenborn

              Comment

              • emerth

                #8
                Re: How to use COM in php?

                Default User <first.last@boe ing.com.invalid > wrote in message news:<40ED8CDD. 1B69E25B@boeing .com.invalid>.. .[color=blue]
                > emerth wrote:
                >[/color]
                ...[color=blue]
                >[color=green]
                > > The memory tekst points to go
                > > out of scope when the function returns.[/color]
                >
                > That's wrong. tekst is initialized to a string literal, which is not
                > automatic storage. String literals have static duration, they exist for
                > the life of the program. The pointer itself is in automatic storage, but
                > a copy of it is returned, so no problem.
                >
                > This would be a problem:
                >
                > char* CArchCtrl::myhe llofunction(cha r* c)
                > {
                > char tekst[] = "Hello ";
                > return tekst;
                > }
                >[/color]

                Yup. I was being careless.
                [color=blue]
                >[color=green]
                > > Your returned pointer will not
                > > point to valid data, which will crash your code when you later try to
                > > use the data.[/color]
                >
                > There is no guarantee that undefined behavior will cause a crash.[/color]

                Sure, but there's no guarantee it won't. And the possibilities you
                list below, while not crashes per se, are IMHO as bad as a crash.
                [color=blue]
                > Another possibility is that it will seem to run fine until you make some
                > seemingly unrelated change, then crash. Or it may just overwrite other
                > automatic data, silently changing values. Don't do it, and don't count
                > the compiler or the run-time environment catching it.
                >[color=green]
                > > Allocate the space for your char string either:
                > > - using new[/color]
                >
                > Ok.
                >[color=green]
                > > - or, make it a class member vavriable[/color]
                >
                > How does that allocate space?[/color]

                Declaring a char array as a class member variable allocates storage
                when an object of that class is instantiated.

                class yadda {
                protected:
                char an_array[64]; // allocated at class instantiation
                char *a_pointer; // not allocation at instantiation - it's just a
                pointer
                }
                [color=blue]
                >[color=green]
                > > - or, have the calling code provide a pointer to space that it manages[/color]
                >
                > The best solution is to use the std::string library container.[/color]

                Certainly. But a std::string allocated on the stack would have led to
                scope issues as well, when the f'n returned.

                There are fine points that exist in C++ or C that never arise in
                script languages like PHP. I just wanted to point some of them out to
                the original poster.

                Comment

                • Default User

                  #9
                  Re: How to use COM in php?

                  emerth wrote:[color=blue]
                  >
                  > Default User <first.last@boe ing.com.invalid > wrote in message news:<40ED8CDD. 1B69E25B@boeing .com.invalid>.. .[/color]

                  [color=blue][color=green]
                  > > There is no guarantee that undefined behavior will cause a crash.[/color]
                  >
                  > Sure, but there's no guarantee it won't. And the possibilities you
                  > list below, while not crashes per se, are IMHO as bad as a crash.[/color]

                  However, that sort of thinking leads newbies down the path of trouble. A
                  common complaint, "I did [something bad] but my program didn't crash, I
                  thought it was supposed to!"
                  [color=blue][color=green][color=darkred]
                  > > > - or, make it a class member vavriable[/color]
                  > >
                  > > How does that allocate space?[/color]
                  >
                  > Declaring a char array as a class member variable allocates storage
                  > when an object of that class is instantiated.
                  >
                  > class yadda {
                  > protected:
                  > char an_array[64]; // allocated at class instantiation
                  > char *a_pointer; // not allocation at instantiation - it's just a
                  > pointer
                  > }[/color]

                  But that's not what you said the first time. Changing the char* to an
                  array of char is what did it. Whether it's a class member or not has
                  nothing to do with it.

                  [color=blue][color=green]
                  > >[color=darkred]
                  > > > - or, have the calling code provide a pointer to space that it manages[/color]
                  > >
                  > > The best solution is to use the std::string library container.[/color]
                  >
                  > Certainly. But a std::string allocated on the stack would have led to
                  > scope issues as well, when the f'n returned.[/color]

                  Not at all. It would be returned by value, meaning it would use copy
                  semantics. It's no more a problem than this:

                  int func()
                  {
                  int i = 3;

                  return i;
                  }

                  You could not return a pointer or reference to an automatic std::string,
                  but return by value is no problem. As I said, to save copying you could
                  pass in a reference to a std::string.
                  [color=blue]
                  > There are fine points that exist in C++ or C that never arise in
                  > script languages like PHP. I just wanted to point some of them out to
                  > the original poster.[/color]

                  Understood, but it's good to get them right. Using std::string and other
                  container classes make C++ much more like PHP than if you use C-style
                  constructs such as char*.



                  Brian Rodenborn

                  Comment

                  Working...