By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,614 Members | 1,680 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,614 IT Pros & Developers. It's quick & easy.

How to use COM in php?

P: n/a
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.CArchCtrl); // OK
$tekst = $mycom->myhellofunction("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::myhellofunction(char* c)
{
char* tekst = "Hello ";
strcat(tekst, c);
return tekst;
}
Jul 17 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a
"Darek" <in*********@NOSPAMpoczta.onet.pl> wrote in message
news:cc**********@nemesis.news.tpi.pl...
In Visual C++, I used project "MFC ActiveX ControlWizard" and I added one
method in class CArchCtrl, in the following way:

char* CArchCtrl::myhellofunction(char* c)
{
char* tekst = "Hello ";
strcat(tekst, c);
return tekst;
}


I'm rather rusty with COM, but that doesn't look like a dispatchable
function to me. Shouldn't it be HRESULT CArchCtrl::myhellfunction(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/
Jul 17 '05 #2

P: n/a
"Darek" <in*********@NOSPAMpoczta.onet.pl> wrote in message news:<cc**********@nemesis.news.tpi.pl>...
----
$mycom = new COM("arch.CArchCtrl); // OK
$tekst = $mycom->myhellofunction("Piotr"); // ERROR
print($tekst);
$mycom = null;
----

Could it be because of the missing "
$mycom = new COM("arch.CArchCtrl);
should be?
$mycom = new COM("arch.CArchCtrl");
Jul 17 '05 #3

P: n/a

User "Harry" <ha*********@hotmail.com> wrote:
$mycom = new COM("arch.CArchCtrl");


--
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
Jul 17 '05 #4

P: n/a
"Darek" <in*********@NOSPAMpoczta.onet.pl> wrote in message news:<cc**********@nemesis.news.tpi.pl>...
Hello,

I have just created new my own activeX, named "arg.ocx", using Visual C++ 6.
<snip>

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

char* CArchCtrl::myhellofunction(char* c)
{
char* tekst = "Hello ";
strcat(tekst, c); ^^^^^^^^^^^^^^^^^
This is illegal in C++ for a few reasons... return tekst;
}


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::myhellofunction(char* 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
Jul 17 '05 #5

P: n/a
"Darek" <in*********@NOSPAMpoczta.onet.pl> wrote in message news:<cc**********@nemesis.news.tpi.pl>...
Hello,

I have just created new my own activeX, named "arg.ocx", using Visual C++ 6.
....
In Visual C++, I used project "MFC ActiveX ControlWizard" and I added one
method in class CArchCtrl, in the following way:

char* CArchCtrl::myhellofunction(char* c)
{
char* tekst = "Hello ";
strcat(tekst, c);
return tekst;
}


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
Jul 17 '05 #6

P: n/a
emerth wrote:
char* CArchCtrl::myhellofunction(char* c)
{
char* tekst = "Hello ";
strcat(tekst, c);
return tekst;
}
And, I may as well point out that returning a pointer to storage
allocated on the stack is a Bad Idea.


That's true.
The memory tekst points to go
out of scope when the function returns.
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::myhellofunction(char* 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.
Your returned pointer will not
point to valid data, which will crash your code when you later try to
use the data.
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.
Allocate the space for your char string either:
- using new
Ok.
- or, make it a class member vavriable
How does that allocate space?
- or, have the calling code provide a pointer to space that it manages


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

#include <string>

std::string CArchCtrl::myhellofunction(char* 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
Jul 17 '05 #7

P: n/a
Default User <fi********@boeing.com.invalid> wrote in message news:<40***************@boeing.com.invalid>...
emerth wrote:
...
The memory tekst points to go
out of scope when the function returns.
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::myhellofunction(char* c)
{
char tekst[] = "Hello ";
return tekst;
}


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


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.
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.
Allocate the space for your char string either:
- using new
Ok.
- or, make it a class member vavriable


How does that allocate space?


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
}
- or, have the calling code provide a pointer to space that it manages


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


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.
Jul 17 '05 #8

P: n/a
emerth wrote:

Default User <fi********@boeing.com.invalid> wrote in message news:<40***************@boeing.com.invalid>...
There is no guarantee that undefined behavior will cause a crash.


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.


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!"
- or, make it a class member vavriable


How does that allocate space?


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
}


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.

- or, have the calling code provide a pointer to space that it manages


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


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


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.
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.


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
Jul 17 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.