Hi,
Firstly, this is Win32 console related, otherwise I'd be reading up on the ncurses library..

My program is updating name:value pairs like Name:John

I wish to use WriteConsoleInput to inject the current value into the inputstream so that the user can edit the characters rather than start again. this is definetly in the realms of showy code, but I'm interested in how easy this could be.

When I run the following code, instead of the string value being inputted to the input stream I see ??. I can't tell if the problem is code page related, a stupid error on my part, or something else ( or all of the above :-) )

I should say that the fillBuff function returns what looks like a valid array of INPUT_RECORDS, 2 for each character of value (keydown & keyup).

code extract:

INPUT_RECORD fillBuff(std::string value) {
    size_t len = value.length();
    int y = 0;
    /*INPUT_RECORD *irec = new INPUT_RECORD[(2*len)-1];*/
    INPUT_RECORD irec[10];
    for (int i=0; i < (2*len)-1; i+=2) {
        irec[i].EventType = KEY_EVENT;
        irec[i].Event.KeyEvent.bKeyDown = TRUE;
        irec[i].Event.KeyEvent.dwControlKeyState = 0;
        irec[i].Event.KeyEvent.uChar.AsciiChar = int(value[y]);
        irec[i].Event.KeyEvent.wRepeatCount = 1;
        irec[i].Event.KeyEvent.wVirtualKeyCode = (value[y]); /* virtual keycode is always uppercase */
        irec[i].Event.KeyEvent.wVirtualScanCode = MapVirtualKeyA(irec[i].Event.KeyEvent.wVirtualKeyCode & 0x00ff, 0);
        irec[i+1].EventType = KEY_EVENT;
        irec[i+1].Event.KeyEvent.bKeyDown = FALSE;
        irec[i+1].Event.KeyEvent.dwControlKeyState = 0;
        irec[i+1].Event.KeyEvent.uChar.AsciiChar = int(value[y]);
        irec[i+1].Event.KeyEvent.wRepeatCount = 1;
        irec[i+1].Event.KeyEvent.wVirtualKeyCode = toupper(value[y]);
        irec[i+1].Event.KeyEvent.wVirtualScanCode = MapVirtualKeyA(irec[i+1].Event.KeyEvent.wVirtualKeyCode & 0x00ff, 0);
        y++;
    }
    return irec[0];
}


/***********************************************************/
int main(int argc, char *argv[]) {
  
  string in;
  string value = "aBCD";

  HANDLE conIn;
  DWORD dw;

  size_t len = value.length();

  conIn = GetStdHandle(STD_INPUT_HANDLE);

  INPUT_RECORD *records;

  records = &fillBuff(value);

  WriteConsoleInput(conIn, records, len , &dw);

  in = getchar();

  return 0;
}

line 1: >>INPUT_RECORD fillBuff(std::string value) {
That should be returning a pointer, not a single element of an array

line 42 of main(): >> records = &fillBuff(value);
and that probably doesn't really work the way you think it does either.

line 23: >> return irec[0];
You can't return an object that was created on the stack and is probably why the program is getting junk. Several ways to resolve the problem. And that does not return a pointer.
1) make irec a static array static INPUT_RECORD irec[10]; . Then on line 23 should be this: return irec which will return a pointer to the first element of the array.
2) pass irec as a parameter INPUT_RECORD fillBuff(std::string value, INPUT_RECORD* irec) { 3) declare irec as a pointer inside fillBuff() and allocate it with new

As you can see I'm still getting my head round pointers, particularly in terms of arrays and dynamic memory allocation.

From your suggestions I just corrected the prototype declaration to return a poitner type rather than just an individual INPUT_RECORD.

Can I confirm that this makes sense:

MYTYPE* getMyTypePNTR(int size) {
    MYTYPE *ptr_mytypeArray = new MYTYPE[size];
    return  ptr_mytypeArray;
}

int main() {
    MYTYPE *ptr-result;
    ptr_result = getMyTypePNTR(value/2);
/* presuming you can cout << MYTYPE */
    cout << *ptr_result;
    cout << *(ptr_result++);
    etc...
    return 0;
}

At this point I see that my char -> INPUT_RECORD machine isn't working as well as I thought...

I will place a message on the MS forums to see if someone can help out. The main problem is generating the correct VirtualKeyCode, and VirtualScanCode. I was under the impression the virtualKeyCode matched the ascii value for UPPERCASE alpha chars. so I was just touppering(char).

I now use MapVirtualKeyA(virtualkey, MAPVK_VK_TO_VSC) to generate the scancode, but the result doesn't seem to match one's for INPUT_RECORDS that I look at during debugging a different program.

Sorry for being so vague here, this is more of a hack and slash coding effort, so maybe I should give it up!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.