ktsangop 23 Junior Poster in Training

Thanks!
As always using the right keyword in a google search can do magic!

I've found a nice function that executes a command line and returns the output to a CString using CreateProcess and pipes.

Link:
http://snipplr.com/view/35935/mfc-run-command-such-as-ping-127001-and-get-stdout-in-a-cstring/

Credits to the author.

ktsangop 23 Junior Poster in Training

Can anyone please give me a guideline on how to code the following?

I have an MFC application which should call on a timer basis a python script from command line and read the ouput of that command line.

I thought of using CreateProcess, but i have no clue of how am i going to read the output of the python script which is sent to the command line buffer.

Is there any standard way of doing this?

Thanks in advance.

ktsangop 23 Junior Poster in Training

For anyone interested :
The answer is yes you can!
Using the driver provided by Inpout32 DLL found here :
Inpout32 Library Information
It's also a nice way to practice on LPT interface programing without the need for external devices such as oscilloscopes....
Just short circuit some output with some input pins and use the functions provided by Inpout32 Library to send and receive data through your parallel port.

ktsangop 23 Junior Poster in Training

Hi everyone!
Just wanted to ask before i begin messing around.

Is it possible for two processes to access the parallel port simultaneously?
So that if i short circuit Input pins to output pins of the parallel port one process will provide input to another one?
I am using Windows XP and C++.
The first process is a commercial one which cannot be modified.

In my experience with serial port it was impossible to interface it when another process had the access.

Thanks in advance!

ktsangop 23 Junior Poster in Training

Do you mean microsoft's forum, or daniweb's windows section?

ktsangop 23 Junior Poster in Training

Update:

After some debugging i came across this:

When eplorer.exe is not loaded during windows startup, DLL_PROCESS_DETACH is never called in my dll although i call FreeLibrary() function inside my application.
To emphasize that, i repeat that none of this problems occurs when windows starts normally with explorer.exe.

Anyone got a clue?
Please help...

Thanks in advance!

ktsangop 23 Junior Poster in Training

Hi everyone.

I'm facing a tricky problem with keyboard hooks in visual c++ (MFC).

I have two global keyboard hooks set in my application and everything works smoothly when windows explorer is running.
But my application needs to run without windows explorer (in kiosk mode), so i replace explorer.exe with my application in the following registry key :

[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon]
"Shell"="myapp.exe" (INSTEAD OF "explorer.exe")

On this situation my hooks stop working after a key press or two.

I'm pretty sure disabling explorer.exe as windows shell has something to do with it because i've been experiencing other similar problems on commercial applications too (not having to do with hooks although).

If anyone has got a clue... i'd be really thankful!

ktsangop 23 Junior Poster in Training

The more i C the less i see!
:D
Cool stuff... thanks!

Kareem Klas commented: Nice word joke. +0
ktsangop 23 Junior Poster in Training

Well that wasn't that easy. Replace WS_EX_APPWINDOW with what?

Anyway i foud another way round.
I made the window handler global, and now i check everytime i call UsbID() if it exists so that no new window is created every time.

But this is just a trick...
I would really like to know why dialogs which are invisible behave that way.
It is not the first time i ran into this problem. Invisible windows always mess things up.

If someone has any other idea of how to properly create and destroy invisible dialogs please help.

Otherwise i will just mark the topic as solved in a couple of days.

Thanks in advance.

ktsangop 23 Junior Poster in Training

Don't really know if WS_EX_APPWINDOW is needed.
Will try to remove it and see what's happening.

Will post back.
Thanks

ktsangop 23 Junior Poster in Training

You should start by reading about Hooks in MSDN.

http://msdn.microsoft.com/en-us/library/ms644959%28v=VS.85%29.aspx

There's no easy and universal way to create a keylogger. It all depends on what and how you want to do something...

Anyway, be also prepared to read about creating DLLs because that's the only way to create GLOBAL hooks. If your app is going to be invisible a global hook is mandatory.

And don't expect to get any help you if you want to create a virus or something...no offence...

ktsangop 23 Junior Poster in Training

Hi everyone!

My function UsbID uses the libusb library (http://www.libusb.org/) to get a flash disk's serial number.

Libusb needs for some reason a Window created or else it doesn't work properly for me.
So i created a CWnd inside my UsbID function which is invisible.

My problem is that if i Destroy and Unregister this window after my job is done, i am getting an -1 return on a following CDialog:: DoModal() function call.
The CDialog has nothing to do with the previous mentioned CWnd.

If i don't Destroy and Unregister the window everything works fine. :S

Please take a look at the followin code and tell me if something is missing or incorrect.

char* UsbID(int len, HINSTANCE appinst)//appinst is the HINSTANCE of main application
{

////////////////////////////////////////////////////////////////////////////////
//CREATE A WINDOW WHICH IS INVISIBLE
    HINSTANCE instance=appinst;

	WNDCLASSEX win_class;

    win_class.cbSize = sizeof(WNDCLASSEX);

    win_class.style = CS_HREDRAW | CS_VREDRAW | CS_GLOBALCLASS ;
    win_class.lpfnWndProc = win_proc1;
    win_class.cbClsExtra = 0;
    win_class.cbWndExtra = 0;
    win_class.hInstance = instance;
    win_class.hIcon = NULL;
    win_class.hCursor = LoadCursor(NULL, IDC_ARROW);
    win_class.hbrBackground = (HBRUSH)(COLOR_3DFACE + 1);
    win_class.lpszMenuName = NULL;
    win_class.lpszClassName = "test";
    win_class.hIconSm = NULL;

    RegisterClassEx(&win_class);

    main_win = CreateWindowEx(WS_EX_APPWINDOW| WS_EX_CONTROLPARENT,
                              "test",
                              "TestLibUsb - Windows Version",
                              WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN
                              | WS_DLGFRAME,
                              CW_USEDEFAULT, 0, 500, 500, NULL, NULL,
                              instance, NULL);
    if (!main_win)
    {
        return FALSE;
    }

	ShowWindow(main_win, SW_HIDE);
//
////////////////////////////////////////////////////////////////////////////////
	char *tt;
	tt=(char *)malloc(len);


//LIB USB FUNCTIONS
    usb_set_debug(4);
    usb_init();
    usb_find_busses();
	char instr[5000];
	get_usbID(instr,sizeof(instr));

//... OTHER CODE...//
	


///////////////////////////////////////
//DESTROY WINDOW
	int error = DestroyWindow(main_win);
	int error2 = …
ktsangop 23 Junior Poster in Training

Got another way around.
Solved.
Thanks

ktsangop 23 Junior Poster in Training

Hi everyone.

I have an MFC project in Visual C++ 6 and was wondering in which way can i detect the level of audio output.

I want to execute some code only when no sound is sent to the audio output.

Could anyone please give me a clue of how to get a small sound sample (say 200 ms) and check if sound level is zero (or if this is impossible, close to zero).

Thanks in advance.

ktsangop 23 Junior Poster in Training

Yeah i did this.
It works fine i just wanted to be sure that using lockfile in cygwin ensures that no concurrent access will be ever possible.

Thanks

ktsangop 23 Junior Poster in Training

Sorry i had to bump...!
:)

Anyone please..?

ktsangop 23 Junior Poster in Training

My problem is that modules like posixfile, fcntl, etc.. need a file handle as an argument.

ConfigObj does not return a file handle, but creates an object which contains the data of the original file, and all the changes made to that object are copied back to the file only when you call :

config.write()

I am thinking of using an extra (dummy) file only to be used as a lock.
Thus, the process that acquires the lock file is the only one that can update the ini file i want.

Can i be sure that this is going to be safe that way?
Or the concurrency problem is simply transfered in the lock file?

I'm thinking that since cygwin is not SMP capable, the requests for acquiring the lock file are serialized so there will be no "real" concurrent access atempt.
If this is correct, my problem is solved...so the question now is :
"Is it correct???"

:)

ktsangop 23 Junior Poster in Training

Hello everyone!

I am using python 2.6 on cygwin environment and wondering how could i prevent two python processes from writing to a file at the same time.

The file that is shared between the python processes is an ini file and is accessed through ConfigObj module.

The first python process is a daemon which runs indefinitely writing to the ini file, data on some random external conditions.

The other python processes, start occasionally (one at a time), execute their code, write data on the ini file and stop.

The command i want to ensure that is safely executed in all scripts is the :

config.write()

with config being the object that is created by the ConfigObj library :

config = ConfigObj('myfile.ini')

Testing the above, didn't produce any errors but, i can never be sure that way...

So, is there any way to do it without rewriting all my code?(Without using threads for example)

P.S. I will probably sooner or later change platform from cygwin to linux, so if there is a solution not applied in cygwin but linux compatible, i will not exlude it.

Thanks in advance.

ktsangop 23 Junior Poster in Training

Then this code you gave:

myvalue = 16

command2 = r"\x03\xa2\x%02x" % myvalue

# JUST OUTPUT THE STRING INSTEAD OF PRINTING IT
command2  #\\x03\\xa2\\x10

should not give any display. See what I mean?

I just used the shell to show what is really stored in command2 variable.
The above code inside a script was also sending the wrong bytes to the serial interface.
Anyway, as i said i messed up with the hex representation...

ktsangop 23 Junior Poster in Training

Directed at ktsangop,

Are you writing your program with an editor, save it as a .py file and then run it, or are you simply running it directly from the Python interpretive shell?

If you run it from the shell, we can argue till the cows lay eggs.

Of course i write python scripts... i just use the shell to test fast some functions AFTER i find bugs in my script...

For me you look like mixing the chr() with hexadesimal numbers and string escapes. Is this not correct?

x=0xb5
part1 = '\x03\xa2' ## two chars chr(0x3) and chr(0xa2)
part2 = chr(x) ## ALSO CHAR
command2 = part1 + part2
print repr(command2)

"""Output:
'\x03\xa2\xb5'
>>> """

Thanks a million tonyjv!!!
I messed up the hex, char etc representations... i always do...
:)
My script runs smoothly now.

Thanks everybody for helping me out.
Cheers

ktsangop 23 Junior Poster in Training

As far as I can tell, both of these options work.

x=16
backslash = chr(92)
part1 = '\x03\xa2'
part2 = hex(x)[1:]
 
command2 = "%s%s%s" % (part1, backslash, part2)
command3 = part1 + backslash + part2

None of them worked...
still the serial transmission :
ser.write(command2) or ser.write(command3) sends the wrong number (92)

ktsangop 23 Junior Poster in Training

Ok, i probably didn't make it clear enough.
Sorry about this.

I have :

...
ser = serial.Serial()
ser.port = 0
ser.baudrate=38400
ser.open()
...

x=16
part1 = '\x03\xa2'
part2 = hex(x)[1:]

command2 = part1 + '\\' + part2

...
#and then 
ser.write(command2) #FAILS because as i said command2 = '\x03\xa2\\x10'
# same thing if i use raw string.
...

And it makes no difference if i use the python shell or pass it on as a Python program.

ktsangop 23 Junior Poster in Training

Try something like string formatting ...

myvalue = 16

command2 = r"\x03\xa2\x%02x" % myvalue

print command2  # \x03\xa2\x10

Does not work either
Print function "escapes" '\\' correctly
But if you type :

myvalue = 16

command2 = r"\x03\xa2\x%02x" % myvalue

# JUST OUTPUT THE STRING INSTEAD OF PRINTING IT
command2  #\\x03\\xa2\\x10
ktsangop 23 Junior Poster in Training

Use raw string

string = r"Everything here is intepreted as is \crap \ jakflj\ \adf\"

Does not work. Tried it to...

>>> r"\\"
'\\\\'
>>> r"\"
  File "<stdin>", line 1
    r"\"
       ^
SyntaxError: EOL while scanning single-quoted string
ktsangop 23 Junior Poster in Training

Hi everybody!

I'm facing a really tricky problem in python.

I am using the pyserial package to communicate with an external serial interface board.
The board accepts certain hexadecimal strings as commands in the following form :
--example--
command1 : "\x02\xa1"
command2 : "\x03\xa2\xb5"
etc...

Some of the commands are static (as command1) and some other are dynamic (as command2).
Dynamic means that for command2 the part of the string "\x03\xa2" is always the same BUT "\xb5" is a variable passed to script (say as an iteger parsed from an txt file).

What i need to do is to concatenate :
"\x03\xa2" with "\xb5" (or anything that the second part of the script might be).

I tried :
x=16 ===> INPUT TO THE SCRIPT
part1 = '\x03\xa2' ===> CONSTANT
part2 = hex(x)[1:] ===> RESULTS IN 'x10'

command2 = part1 + '\\' + part2 ====> RESULTS IN '\x03\xa2\\x10'

Which is incorrect because the boards understands the extra backslash as : hexadecimal (5C) = decimal (92)
So the wrong number (92 instead of 16) is sent.

Is there ANY way to add a single backslash to this string concatenation??

Thanks in advance!

ktsangop 23 Junior Poster in Training

Nice, now it's perfectly clear!

Every day i come to realise more and more that theory is more important than practice in software development...
:)

Thanks a lot.

ktsangop 23 Junior Poster in Training

Ohh... nice... now i will never be sure about my calculations
:(
...unless i always use the decimal module...

Thanks a lot for the enlightenment...
:)

ktsangop 23 Junior Poster in Training

Hi everybody.
Could someone explain me the usage of python's modulo operator and/or the fmod function?

why do i get these results?

>>> 23%0.1
0.099999999999998729
>>> import math
>>> math.fmod(23,0.1)
0.099999999999998729
>>>

normally 23 mod 0.1 = 0 because 23 / 0.1 = 230 exactly
is there anything wrong there or did i miss something???

Thanks in advance...

ktsangop 23 Junior Poster in Training

Can someone find my error on the following code?
On execution i get the error :
Table 'base1.tablename' doesn't exist

CREATE DEFINER=`root`@`localhost` PROCEDURE `proc5`()
BEGIN
  DECLARE done BOOL DEFAULT FALSE;
  DECLARE tablename VARCHAR (20);
  DECLARE tracktables CURSOR FOR
  SELECT table_name FROM INFORMATION_SCHEMA.tables WHERE table_name LIKE 'track%';
  DECLARE CONTINUE HANDLER FOR SQLSTATE '02000'
    SET done = TRUE;

  OPEN tracktables;

  myloop: LOOP
    FETCH tracktables INTO tablename;
    IF done THEN
      CLOSE tracktables;
      LEAVE myloop;
    END IF;

    SELECT ammount from tablename;
  END LOOP;
END

I suppose that my usage of tablename cursor isn't correct but how should i use it?

Thanks in advance..

ktsangop 23 Junior Poster in Training

Thanks for the answers.
It seems like i have to use cursors which i am not familiar with but it is time for me to learn..

The following code

(SELECT ammount FROM track1) UNION (SELECT ammount FROM track2) UNION (SELECT ammount FROM track3)

requires that you know which tables you have to select. I want it to be dynamic cause the number of tables beginning with 'track' changes all the time.

I found that this returns the tables i want to use :

SELECT table_name FROM INFORMATION_SCHEMA.tables WHERE table_name LIKE 'track%'

Now i am trying to find a way to execute a query seperately in all of the tables the first line returns and join the results.

ktsangop 23 Junior Poster in Training

Hello everybody.
I'd like some help with a query.

I have a database named 'base1' which contains many tables.
Some of the tables have names like 'track1' , 'track2' etc... which have identical columns.

I would like to select all the tables beginning with track and show the data of a specific column named 'ammount'.

Thanks in advance.