שימוש ב API לקריאת קובץ INI

eliranitzhak

New member
שימוש ב API לקריאת קובץ INI

שלום לכולם. יש לי את הקוד הבא, שלא עובד. קובץ ה INI עצמו קיים ומכיל נתונים. המשתנה NC מחזיר את אורך המחרוזת מה INI - אבל המשתנה ret (שאמור להכיל את המחרוזת) - ריק. תודה מראש.
Private Sub Command2_Click() ret = String(255, 0) 'Create buffer for string value nc = GetPrivateProfileString("StartUp", "PASSWORD", "NULL", ret, 255, App.Path & "\sample.ini") 'NC , no of character copied to the buffer MsgBox ret & nc End Sub​
 

ארזירון

New member
יש סיכוי סביר שלא....

המחרוזת שלך באורך של 255 תוים שזה קצת מעל מה ש-msgbox יציג לך בצורה סבירה, בייחוד בגלל שכל התוים שם הם null char... הייתי משנה את זה קצת למשהו כמו:
ret = Space(255) ' instead of ret = string(255,0) 'call the function 'trim the result ret = Left(ret, nc) MsgBox ret​
 

eliranitzhak

New member
עדיין בעיה בקריאת קבצי INI

מישהו יכול לראות אם הפרוייקט שצרפתי בהודעה קודמת עובד אצלם, או להעלות לי דוגמה של קריאת INI שעובדת ? אני צריך קריאה בלבד, את הקובץ יערכו עם notepad - ואני טיפה תקוע. תודה מראש.
 

eliranitzhak

New member
תודה, אבל RET עדיין ריק.

הנה הקוד אחרי השינוי. אני רוצה לציין שיצירת ה INI עובדת כראוי, רק הקריאה ממנו לא. למה INI ? זה הכי מתאים למטרה של התוכנה, בהתחשב באלטרנטיבות. למשקיעים, מצורף קובץ ZIP עם הפרוייקט. (זה פרוייקט דמי זמני שימוזג אח"כ לפרוייקט האמיתי) תודה מראש !
ret = Space(255) 'Create buffer for string value nc = GetPrivateProfileString("StartUp", "PASSWORD", "NULL", ret, 255, App.Path & "\sample.ini") 'NC , no of character copied to the buffer MsgBox (ret)​
והנה הקוד המלא, אולי מישהו יצליח למצוא את הבעייה.
Public Declare Function GetPrivateProfileInt Lib "kernel32" Alias "GetPrivateProfileIntA" (ByVal lpApplicationName As String, ByVal lpKeyName As String, ByVal nDefault As Long, ByVal lpFileName As String) As Long Public Declare Function GetPrivateProfileString Lib "kernel32" Alias "GetPrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpDefault As String, ByVal lpReturnedString As String, ByVal nSize As Long, ByVal lpFileName As String) As Long Public Declare Function WritePrivateProfileString Lib "kernel32" Alias "WritePrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpString As Any, ByVal lpFileName As String) As Long Private Sub Command1_Click() MsgBox (GetPrivateProfileInt("StartUp", "USERID", 0, App.Path & "\sample.ini")) End Sub Private Sub Command2_Click() ret = Space(255) 'Create buffer for string value nc = GetPrivateProfileString("StartUp", "PASSWORD", "NULL", ret, 255, App.Path & "\sample.ini") 'NC , no of character copied to the buffer MsgBox (ret) End Sub Private Sub Command3_Click() WritePrivateProfileString "StartUp", "PASSWORD", "system", App.Path & "\sample.ini" End Sub​
 

eliranitzhak

New member
ניסיתי גם עם trim

שכחתי לציין - גם עם שימוש ב trim - המחרוזת עדיין ריקה. לדוגמה
MsgBox (trim(ret))​
 

ארזירון

New member
תנסה משהו

בהגדרה שלך של הפונקציה עצמה - תשנה את ההצהרה על lpKeyName מ-Any ל-String. לא אמור להשפיע, אבל כבר ראיתי דברים מוזרים בחיי....
 

eliranitzhak

New member
ניסיתי, אותה בעייה ממשיכה

nc מכיל את אורך המחרוזת שחיפשתי ב INI ret מחזיר מחרוזת ריקה. אם זו לא יותר מדי טרחה, מישהו יכול להעלות לי דוגמה עובדת לקריאה מקובץ INI ? תודה.
 
למעלה