位置:首頁 > 軟件操作教程 > 工業(yè)產(chǎn)品軟件 > Pro/Engineer > 問題詳情

Proe插件如何讀取配置文件

提問人:劉團(tuán)圓發(fā)布時間:2021-09-10

用Proe二次開發(fā)包開發(fā)出來的應(yīng)用軟件有時候需要保存一些變量的值,不然每次重新啟動Proe后都需要重新設(shè)置一邊效率很低。但是如何實現(xiàn)呢?


我先利用Pro/toolkit API函數(shù)可定無法實現(xiàn),必須要讀取磁盤文件來實現(xiàn)。其實有很多中法式來實現(xiàn)磁盤文件的讀取與寫入。


1)利用C語言提供的,但是自己需要編寫太多的代碼,而起很復(fù)雜。


2)利用C++的文件類來實現(xiàn),也很復(fù)雜而且需要對C++很熟悉。


3)利用Windows API函數(shù)來說實現(xiàn),這個就是利用Windows本身的資源來實現(xiàn)。Windows中配置文件后綴為.ini,文件格式對我們來說無所謂,只要


能實現(xiàn)功能就行。


下面介紹兩個讀取和寫入.ini配置文件的函數(shù)。


BOOL WritePrivateProfileString(

   LPCTSTR lpAppName,

   LPCTSTR lpKeyName,

   LPCTSTR lpString,

   LPCTSTR lpFileName

   );

其中各參數(shù)的意義:


LPCTSTR lpAppName 是INI文件中的一個字段名.


LPCTSTR lpKeyName 是lpAppName下的一個鍵名,通俗講就是變量名.


LPCTSTR lpString 是鍵值,也就是變量的值,不過必須為LPCTSTR型或CString型的.


LPCTSTR lpFileName 是完整的INI文件名.


設(shè)現(xiàn)有一名學(xué)生,需把他的姓名和年齡寫入 c:studstudent.ini 文件中.


CString strName,strTemp;

int nAge;

strName=”張三”;

nAge=12;

::WritePrivateProfileString(“StudentInfo”,”Name”,strName,”c:studstudent.ini”);


此時c:studstudent.ini文件中的內(nèi)容如下:


[StudentInfo]

Name=張三


要將學(xué)生的年齡保存下來,只需將整型的值變?yōu)樽址图纯?


strTemp.format(“%d”,nAge);


::WritePrivateProfileString(“StudentInfo”,”Age”,strTemp,”c:studstudent.ini”);


DWORD GetPrivateProfileString(

  LPCTSTR lpAppName,

  LPCTSTR lpKeyName,

  LPCTSTR lpDefault,

  LPTSTR lpReturnedString,

  DWORD nSize,

  LPCTSTR lpFileName

  );

其中各參數(shù)的意義:


前二個參數(shù)與 WritePrivateProfileString中的意義一樣.


lpDefault : 如果INI文件中沒有前兩個參數(shù)指定的字段名或鍵名,則將此值賦給變量.


lpReturnedString : 接收INI文件中的值的CString對象,即目的緩存器.


nSize : 目的緩存器的大小.


lpFileName : 是完整的INI文件名.


現(xiàn)要將上一步中寫入的學(xué)生的信息讀入程序中.


CString strStudName;

int nStudAge;

GetPrivateProfileString(“StudentInfo”,”Name”,”默認(rèn)姓名”,strStudName.GetBuffer(MAX_PATH),MAX_PATH,”c:studstudent.ini”);


執(zhí)行后 strStudName 的值為:”張三”,若前兩個參數(shù)有誤,其值為:”默認(rèn)姓名”.


在啟動插件的時候讀取.ini文件中參數(shù)并付給全局變量就行了,同樣還可實現(xiàn)又好的設(shè)置界面。

繼續(xù)查找其他問題的答案?

相關(guān)視頻回答
回復(fù)(0)
返回頂部