|
It really depends.
We purchased an HP unit for our testing. We got the Embedded VC++ 3.0 from MS for $15 (also downloadable to MSDN members).
It is best to assume that much of your UI will have to change. WCE3.0 does support a reasonable version of MFC, but the size restrictions on the UI will require that all your dialogs and windows will probably require some retooling.
Tim Smith
"Programmers are always surrounded by complexity; we can not avoid it... If our basic tool, the language in which we design and code our programs, is also complicated, the language itself becomes part of the problem rather that part of the solution."
Hoare - 1980 ACM Turing Award Lecture
|
|
|
|
|
Hi,
I have a C API that I want to wrapp in C++.
The idea I have is to have a C++ Base Class and have the C API call virtual functions of the base class, then the derived class can customise these functions.
The only way i can think of doing this is to have a single global pointer to the base class and have the C code call the member functions of that global object. So before I can call certain functions linked to the C API I have to make sure this global pointer points to the right instantiation of the derived class...
Its not very elegant or clean, i was wondering if anybody had any better ideas.
Asim Hussain
e: asim@jawache.net
w: www.jawache.net
|
|
|
|
|
Confused as to what you mean here:
1) You want a C++ class to wrap C functions. In this case, you dont need to do anything special
class MySleep
{
public:
void GoSleep (int x) { sleep (x);};
};
2) You want C functions to access C++ classes (callbacks, threads?)
If you can pass a parameter around then you can pass the 'this' pointer as a parameter to a static function which calls the appropriate virtual function of 'this'
class MyThread
{
public:
static void threadHelper (void *param);
virtual void onStart ();
};
void MyThread::threadHelper (void *param)
{
((MyThread *)param)->onStart ();
}
.
.
.
create_thread (MyThread::threadHelper, this);
There are undoubtedly many other ways of doing this as well
|
|
|
|
|
Thanks,
Yes it was section 2, I want the the C Functions to Call C++ Functions.
The problem is still that the C Functions need to know the 'this' parameter to pass to the static function, therefore you still have to specify a global variable for the C Functions to use.
I've now implemented a static member function that returns an instance of the derived class pointer in the base class. Its still not pretty since I still have to make sure the derived class sets that pointer to itself before the C Functions can call it, but its better than making sure a global pointer is set.
Cheers
Asim Hussain
e: asim@jawache.net
w: www.jawache.net
|
|
|
|
|
It's still not completly clear what you try to achive.
You have C functions that need to call member functions of an object. Is their any special reason why this functions have to be C functions or you can not pass them the instance pointer of the object to call?
If the C function is a callback you want to redirect into a member function, you should take a look at this article[^].
--
Daniel Lohmann
http://www.losoft.de
(Hey, this page is worth looking! You can find some free and handy NT tools there )
|
|
|
|
|
I am extending and embedding the Python interpreter into my application,
Python is a scripting language, I have written extentions to it in C, I now want these C extention functions to call C++ functions of a base class.
class Base {
// .....
virtual void func (char * line) { };
//....
class Derived: public Base {
//....
virtual void func (char * line) { printf(line); }
//....
extern "C" static pyFunc (...) {
// Somehow call the func pointer of the base class
}
When the a certain function in the script is called it calls 'pyFunc' I want pyFunc to then call Base::func (and then the Dervied::func)
There will only ever be a single instance of the Derived class, so before I had instantiated it as a global object, then called the 'Derived.func' function from that global object in the C functions. I can't instatinate and object of the base class since only the base classes member functions will be called.
I want the C Functions to call functions of the base class but the functions deffered to those of the derived class.
It works but it is messy, i was just wondering if there was a cleaner way of doing it?
Asim Hussain
e: asim@jawache.net
w: www.jawache.net
|
|
|
|
|
I'm not sure what your problem is. By the mechanism of virtual functions, if you call func on a Derived object, it is Derived::func that is executed. If you want the base implementation to be executed as well, imbed a call to it into the derived func :
class Derived: public Base {
virtual void func (char * line) {
Base::func(line);
printf(line);
}
Joaquín M López Muñoz
Telefónica, Investigación y Desarrollo
|
|
|
|
|
Sounds to be a classical instantiation of the Singleton pattern. Therefore your approach with the static function to retrieve the one and only instance is quite fine. To make sure that there is never (even accidentally) another instance, you should declare the constructor as protected. The following shows the principle:
class Base
{
public:
static Base& Instance()
{
ASSERT( Base::pInstance );
return *Base::pInstance;
}
virtual void f();
virtual void g();
...
protected:
Base()
{
...
}
Base( const Base& );
static Base* pInstance;
};
class Derived : public Base
{
public:
virtual void f();
...
static void CreateTheInstance()
{
ASSERT( Base::pInstance == 0 );
Base::pInstance = new Derived();
}
protected:
Derived()
{
...
}
};
During initialization or at some other appropriate point in time you have to call Derived::CreateTheInstance() to construct the one and only instance, that is afterwards accessable via Base::Instance().
--
Daniel Lohmann
http://www.losoft.de
(Hey, this page is worth looking! You can find some free and handy NT tools there )
|
|
|
|
|
Excellent,
Thanks just what I wanted.
Asim Hussain
e: asim@jawache.net
w: www.jawache.net
|
|
|
|
|
I would like to use a function that returns a integer value between two values. I met the rand() function, but I need a value in a range.
[]'s
Cristiano.
|
|
|
|
|
|
Or easier,
rand () % n
If you want a range between A and B then just shift everything along:
int value = a + (rand () % (b - a));
|
|
|
|
|
This not work corretly. The returned value is zero.
[]'s
|
|
|
|
|
The following will print 100 random values between 120 and 320
int getRange (int low, int high)
{
return low + (rand () % (high - low));
}
int main (int argc, char *argv[])
{
srand (time (NULL));
for (int x = 0; x < 100; x++)
printf ("Random Value %d = %d\n", x, getRange (120, 320));
return 0;
}
|
|
|
|
|
Almost...
The problem is:
int rand(void);
so it should be:
rand()*n/RAND_MAX
or
((double)rand()/RAND_MAX)*n
Railgun is like a Gilette Mach 3 - it does the job with one, easy stroke.
|
|
|
|
|
This is not correct. The expression yields a value between 0 and n, but the probability of n is miniscule (1/(RAND_MAX +1), to be precise), which is not the probability for the other values. The following return a random value between 0 and n with uniform distribution:
int randn(int n)
{
int r;
while((r=rand())==RAND_MAX);
return ((double)r/RAND_MAX)*(n+1);
}
Joaquín M López Muñoz
Telefónica, Investigación y Desarrollo
|
|
|
|
|
When using a variable argument list in a function is there a safe way determine how many arguments there are and what type they are before getting them off list?
Happy programming!!
|
|
|
|
|
try _vsnprintf, _vsnwprintf.
bibamus, edamus, cras moriemur [eat, drink, for tomorrow we die]
|
|
|
|
|
There isn't any way to do this. printf and routines like it are VERY dependent on the format string telling the routine exactly what to expect in the calling arguments. Thus, if your calling arguments don't match the format string, you get a crash.
In other cases, you might use a NULL value to terminate the argument list. Or you could even have a count at the start of the argument list. It is really up to you exactly what you want to do.
The only thing you have to remember is that there is no way to get the real number of arguments on the stack amd you can't tell what types of arguments were placed on the stack.
Tim Smith
"Programmers are always surrounded by complexity; we can not avoid it... If our basic tool, the language in which we design and code our programs, is also complicated, the language itself becomes part of the problem rather that part of the solution."
Hoare - 1980 ACM Turing Award Lecture
|
|
|
|
|
According to the spec for var args:
It is upto the calling routine to specify in some manner how many arguments there are, since it is not always possible to determine the number of arguments from the stack frame
Best way would be to pass a parameter before the var args specifying how many arguments are found or, like sprintf, just trawl through the arguments and hope they are all there. Neither are particulary robust...
|
|
|
|
|
How can I get my IP (dotted format) programmatically (I'm using Dialup Modem if this matters, <ye ye,="" don't="" laugh="">)?
--BlackSmith--
"With the help of all mighty", 2001, Me.
|
|
|
|
|
gethostname()
bibamus, edamus, cras moriemur [eat, drink, for tomorrow we die]
|
|
|
|
|
gethostname() retrieves my machine/username/(watever...)
I need my IP number (255.23.01.121...) when I'm connected to the INET,
the one appears in ipconfig command under IP Address section.
I hope my Q is clearer now .
10X.
--BlackSmith--
"With the help of all mighty", 2001, Me.
|
|
|
|
|
There may well be some nice Win32 functions, but using the Unix socket functions:
struct hostent *ent = gethostbyname (gethostname ());
struct in_addr *in = (struct in_addr *)ent->h_addr_list[0];
printf ("My host IP is %s\n", inet_ntoa (*in));
Very messy I know, and with a distinct lack of error checking
|
|
|
|
|
i don't know how to get it in c++ under windows, but i know in java it is very easy, if help, i will post code for u.
cheers
includeh10
|
|
|
|