我在CTESTApp::InitInstance()里面: free((void*)m_pszRegistryKey);
//再清除Ini变量
free((void*)m_pszProfileName);
//改变Ini文件名.
m_pszProfileName=_tcsdup(m_strProfile);
然后调用CString tmp = GetProfileString("Set", "name", "default");可以得到tmp = "小强";
但是我在CMainFrame::OnCreate里面:CTESTApp* app = (CTESTApp*)AfxGetApp();CString tmp = app->GetProfileString("Set", "name", "default");得到的tmp总是default。为什么啊?求高手指教

解决方案 »

  1.   

    红色字部分是我的代码,其他都是MFC初始代码,整个工程只有这2个函数我加代码了,其他都没动int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
    {
    if (CMDIFrameWnd::OnCreate(lpCreateStruct) == -1)
    return -1;
    // CString tmp = AfxGetApp()->GetProfileString("Sett","Name","default");
    CString tmp = theApp.GetProfileString("Set","Name","default");
    AfxMessageBox(tmp);
    }BOOL CMy46546531321321App::InitInstance()
    {
    AfxEnableControlContainer(); // Standard initialization
    // If you are not using these features and wish to reduce the size
    //  of your final executable, you should remove from the following
    //  the specific initialization routines you do not need. //首先清除注册表键值变量所分配的内存空间 .
    free((void*)m_pszRegistryKey);
    //再清除Ini变量
    free((void*)m_pszProfileName);
    //改变Ini文件名.
    m_pszProfileName=_tcsdup("D:\\MyConfig.ini");
    CString tmp1 = GetProfileString("Set","Name","default");
    AfxMessageBox(tmp1);
    #ifdef _AFXDLL
    Enable3dControls(); // Call this when using MFC in a shared DLL
    #else
    Enable3dControlsStatic(); // Call this when linking to MFC statically
    #endif // Change the registry key under which our settings are stored.
    // TODO: You should modify this string to be something appropriate
    // such as the name of your company or organization.
    SetRegistryKey(_T("Local AppWizard-Generated Applications")); LoadStdProfileSettings();  // Load standard INI file options (including MRU) // Register the application's document templates.  Document templates
    //  serve as the connection between documents, frame windows and views. CMultiDocTemplate* pDocTemplate;
    pDocTemplate = new CMultiDocTemplate(
    IDR_MY4654TYPE,
    RUNTIME_CLASS(CMy46546531321321Doc),
    RUNTIME_CLASS(CChildFrame), // custom MDI child frame
    RUNTIME_CLASS(CMy46546531321321View));
    AddDocTemplate(pDocTemplate); // create main MDI Frame window
    CMainFrame* pMainFrame = new CMainFrame;
    if (!pMainFrame->LoadFrame(IDR_MAINFRAME))
    return FALSE;
    m_pMainWnd = pMainFrame; // Parse command line for standard shell commands, DDE, file open
    CCommandLineInfo cmdInfo;
    ParseCommandLine(cmdInfo); // Dispatch commands specified on the command line
    if (!ProcessShellCommand(cmdInfo))
    return FALSE; // The main window has been initialized, so show and update it.
    pMainFrame->ShowWindow(m_nCmdShow);
    pMainFrame->UpdateWindow(); return TRUE;
    }
    在InitInstance()里可以读出tmp “小强”,但是在CMainFrame::OnCreate里读出的是默认值!跪求高手指教!!
      

  2.   

    你是写ini文件还是写注册表,如果是写ini文件就应该把下面这一行注释掉。
    SetRegistryKey(_T("Local AppWizard-Generated Applications"));
      

  3.   

    我是读取自己的INI,问题解决了!谢谢!结贴了。