c++ Code crasht cmd-box - aber ohne nützlichen fehler
-
Hallo Leute,
Ich bin neu auf diesen Forum und Anfänger mit c++, ich habe ein Programm verfasst, doch wenn ich es ausführe dann crasht es meine cmdbox und ich verstehe nicht warum.
Wäre um hilfe sehr Dankbar, hier ist der Sourcecode:
MAC_Toolbar.cpp:
#include "stdafx.h" #include <atlstr.h> #include <Windows.h> #include <string> #include <iostream> #include <sstream> using namespace std; #include "MAC_Variables.h" #include "MAC_inifuncs.h" int _tmain(int argc, _TCHAR* argv[]) { cout << "nText: " << nText << "\n"; nText = "Test\n"; cout << "nText: " << nText; string iTestIniRead=""; // (LPCWSTR retval, LPCWSTR ifilename, LPCWSTR isection, LPCWSTR ikey, LPCWSTR idefault, int isize = 256) //iTestIniRead = iniRead(L"int", L"test.ini", L"test", L"key", 0); cout << "iniRead int: " << iTestIniRead << "\n"; iTestIniRead = iniRead(L"string", L"test.ini", L"test", L"key1", 0); cout << "iniRead str: " << iTestIniRead << "\n"; system("pause>nul"); return 0; }MAC_Variables.h
#ifdef MAC_Rebuild #define EXTERN #else #define EXTERN extern #endif EXTERN std::string nText="Hello World";// = file(;und
MAC_inifuncs.hstring iniRead(LPCWSTR retval, LPCWSTR ifilename, LPCWSTR isection, LPCWSTR ikey, LPCWSTR idefault, int isize = 256) { LPWSTR itemp=0; if (lstrcmp(retval, L"int") ) { std::string itemp2 = CW2A(idefault); // Convert LPCSWSTR to String int idefaultini; // int idefault istringstream(itemp2) >> idefaultini; // Convert String to int int itemp; itemp = GetPrivateProfileInt(isection, ikey, idefaultini, ifilename); // int std::string iret = std::to_string(itemp); return iret; } else if (lstrcmp(retval, L"bool")) { DWORD iret; iret = GetPrivateProfileString(isection, ikey, idefault, itemp, isize, ifilename); // boolean //std::to_string(iretstring) //std::string test = CW2A(itemp); std::string test = std::to_string(iret); return test; } else if (lstrcmp(retval, L"float")) { GetPrivateProfileString(isection, ikey, idefault, itemp, isize, ifilename); // float //std::to_string(iretstring) std::string iret = CW2A(itemp); return iret; // float } else if (lstrcmp(retval, L"string")) { GetPrivateProfileString(isection, ikey, idefault, itemp, isize, ifilename); // float //std::to_string(iretstring) std::string iret = CW2A(itemp); return iret; // float } else { return "Error - Please choose Data type."; } }Der crash kommt immer wenn ich die iniRead function ausführe.
Danke für die Hilfe.
Grüße, Busti.
-
Ich habe nur mal kurz überflogen aber laut msdn muss lpReturnedString ein Pointer sein der auf einen Buffer zeigt.
Aber das ist bei dir einfach nur ein Pointer ohne Wert und du tust so als würde er auf einen 256er Buffer zeigen.//Aufruf LPWSTR itemp=0; GetPrivateProfileString(isection, ikey, idefault, itemp, isize, ifilename); // floatLaut MSDN:
DWORD WINAPI GetPrivateProfileString(
_In_ LPCTSTR lpAppName,
_In_ LPCTSTR lpKeyName,
_In_ LPCTSTR lpDefault,
_Out_ LPTSTR lpReturnedString,
_In_ DWORD nSize,
_In_ LPCTSTR lpFileName
);ReturnedString [out]
A pointer to the buffer that receives the retrieved string.nSize [in]
The size of the buffer pointed to by the lpReturnedString parameter, in characters.P.S.: Ka welche IDE du benutzt, aber in C++ braucht man eine IDE bei der man Breakpoints setzen kann.
Dann koenntest Du Breakpoints setzen und Schritt für Schritt durch die Funktion gehen.
-
gdb funktioniert auch wunderbar aus der Konsole raus, das hat sogar einen TUI-Mode.