As a follow-up to problem locking the mic using code here is a code example of one way it can be done:
- Code: Select all
#include <windows.h>
#include <stdio.h>
#include <ctype.h>
struct command {
const char *text; // in: text to look for
int code; // out: command number
};
command my_commands[] = {
{ "lock mic", 0},
{ "push to talk", 0},
{ "console", 0},
{ "send a file", 0},
{ "all hands", 0},
{ "view webcam", 0},
{ "red dot", 0},
{ NULL, 0} // stop when null
};
struct fill_cmd_param {
command *commands;
int nfound;
};
static void fill_commands(HMENU hMenu, fill_cmd_param *fcpp);
int main(int argc, TCHAR* argv[])
{
HWND hwnd = FindWindow("DlgGroupChat Window Class", NULL);
if (hwnd == NULL)
return 1;
HMENU hmenu = GetMenu(hwnd);
if (hmenu == NULL)
return 2;
fill_cmd_param fcp = {my_commands, 0};
fill_commands(hmenu, &fcp);
if (fcp.nfound > 0) {
printf("found %d commands:\n", fcp.nfound);
for (command *cmd = my_commands; cmd->text != NULL; ++cmd) {
if (cmd->code != 0) {
printf(" %s:\t%d\n", cmd->text, cmd->code);
} else {
printf(" %s:\t(not found)\n", cmd->text);
}
}
}
return 0;
}
// find substring sub in string str, case-insensitive
static
const char *strstri(const char *str, const char *sub)
{
size_t str_len = strlen(str);
size_t sub_len = strlen(sub);
if (sub_len > str_len)
return NULL;
const char *end = str + str_len - sub_len + 1;
for (const char *s = str; s != end; ++s) {
if (toupper(*s) == toupper(*sub) ) {
const char *ssub = &sub[1];
for (const char *ss = &s[1];
*ssub && (toupper(*ss) == toupper(*ssub)); ++ss)
++ssub;
if (*ssub == '\0')
return s;
}
}
return NULL;
}
// walk through all menu items/sub menues and fill the
static
void fill_commands(HMENU hmenu, fill_cmd_param *fcpp)
{
if (hmenu == NULL)
return;
int nitems = GetMenuItemCount(hmenu);
for (int i = 0; i < nitems; ++i) {
UINT mstate = GetMenuState(hmenu, i, MF_BYPOSITION);
if (mstate & MF_POPUP) {
// search sub menu - recursive
fill_commands(GetSubMenu(hmenu, i), fcpp);
} else if (! (mstate & (MF_BITMAP | MF_OWNERDRAW))){ // imply MF_STRING
char str[64];
if (GetMenuString(hmenu, i, str, sizeof(str), MF_BYPOSITION) > 0) {
// match with one of our comma
for (command *cmd = fcpp->commands; cmd->text != NULL; ++cmd) {
if (strstri(str, cmd->text) != NULL) { // match
// get menu cmd
int code = GetMenuItemID(hmenu, i);
if (cmd->code != 0) { // duplicate - which one to choose?
fprintf(stderr, "%s: %d -> %d\n", cmd->text, cmd->code, code);
} else {
cmd->code = code;
++fcpp->nfound;
}
break;
}
}
}
}
}
}
output:
found 6 commands:
lock mic: 33340
push to talk: 33343
console: 32998
send a file: 33231
all hands: 32995
view webcam: 33163
red dot: (not found)



