Problem with edit control

Hey @ all,

i have a big problem with reading characters from a single line edit control.
I want to read the number of characters and the string written in an edit field.Both informations i want to store in an file.The numbers of characters is ok but from the string i just get the fist character...
Where is my mistake?




Source Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include <windows.h>
#include <stdio.h>
#include "functions.h"

//Neues Produkt Dialog
LRESULT CALLBACK neuesProduktProc( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam )
{
  CHAR *lpszName[16]; 
  WORD anzChar;
  FILE *fp;
  switch(msg)  
  {
    case WM_CREATE:
	CreateWindow(TEXT("Edit"), TEXT(""),    
	      WS_VISIBLE | WS_CHILD | WS_BORDER,
	      50, 50, 80, 25,        
	      hwnd, (HMENU) ID_EDITNAME, NULL, NULL);
	CreateWindow(TEXT("Button"), TEXT("Hinzu"),    
	      WS_VISIBLE | WS_CHILD | WS_BORDER,
	      100, 50, 80, 25,        
	      hwnd, (HMENU) ID_EDITHINZU, NULL, NULL); 
	break;

    case WM_COMMAND:
		if(LOWORD(wParam) == ID_EDITHINZU) {
			anzChar = (WORD) SendDlgItemMessage(hwnd, 
                                                ID_EDITNAME, 
                                                EM_LINELENGTH, 
                                                (WPARAM) 0, 
                                                (LPARAM) 0);
			*((LPWORD)lpszName) = anzChar;
			SendDlgItemMessage(hwnd, 
                               ID_EDITNAME, 
                               EM_GETLINE, 
                               (WPARAM) 0,       // line 0 
                               (LPARAM) lpszName); 
			lpszName[anzChar] = '\0'; 
			fp = fopen("test.txt","a");
			if(fp == NULL) {
				MessageBox(hwnd,TEXT("Datei konnte nicht geöffnet werden"),TEXT("Fehler beim Öffnen der Datei"),MB_OK);
			}
			else {
				fprintf(fp,"%i%s",anzChar,lpszName);
				fclose(fp);
			}
		}
	break;

    case WM_CLOSE:
        DestroyWindow(hwnd);
        break;       

  }
  return (DefWindowProc(hwnd, msg, wParam, lParam));

}
That will happen if your environment thinks you're using Unicode and you are using ansi versions of the various C Run Time functions. When the ansi function hits the null byte after the first real byte it will think its the end of the string and you'll only get the first character. Are you using VC9?
I am using the VisualStudio 2008 Professional Edition
That's what I thought. The same thing happened to me when I first got it.

Its default configuration is to define UNICODE. You have to go into the Project Settings and use either character encoding 'Not Set' or 'Multi-byte character set'. Then your program will mysteriously work. Give me a minute and I'll look up the exact menu sequence you need.
Project >> Properties

Dialog Box Comes Up

In left pane click on...

Configuration Properties >> General

In Right Pane You'll see 'Character Set'

Your 'UNICODE' will likely be selected. Change that to either
of the other two, and I think you'll be fine.
Thank you very very much!!!!!!!!!Great work!!!!Erything is fine

THX
Good!

By the way, why don't you check out GetTextLength() and GetWindowText(). I think they are a bit easier to use than the SendMessage() stuff for getting text out of text boxes.
Topic archived. No new replies allowed.