We help IT Professionals succeed at work.
Get Started

GetPrivateProfileInt Doesn't work for me (win XP & Win 7 32bit) - any suggestions

2,004 Views
Last Modified: 2013-12-21
In order to read configuration file I'm using the following win API code Taken from MSDN:

UINT WINAPI GetPrivateProfileInt(
  __in  LPCTSTR lpAppName,
  __in  LPCTSTR lpKeyName,
  __in  INT nDefault,
  __in  LPCTSTR lpFileName
);

BOOL WINAPI WritePrivateProfileString(
  __in  LPCTSTR lpAppName,
  __in  LPCTSTR lpKeyName,
  __in  LPCTSTR lpString,
  __in  LPCTSTR lpFileName
);

From some reason it doesn't work for me.
Below is the code i wrote:

#include <iostream>
#include <Windows.h>

int main(int argc, char *argv[])
{
  char dbserver[1000];
  int dbport;
  char db[] = "database";
  char port[]="port";
  char server[] = "server";
  char ini[] = "Launcher.ini";
  GetPrivateProfileString((LPCWSTR)db, (LPCWSTR)server, (LPCWSTR)"127.0.0.1", (LPWSTR)dbserver, 1000, (LPCWSTR)ini);
  dbport = GetPrivateProfileInt((LPCWSTR)db, (LPCWSTR)port, 143, (LPCWSTR)ini);
   
  return 0;
}

ini file:
------------------------------------------------------------------------
; last modified 1 April 2001 by John Doe
[owner]
name=John Doe
organization=Acme Products
 
[database]
; use IP address in case network name resolution is not working
server=192.0.2.42    
port=143
file = "acme payroll.dat"
------------------------------------------------------------------------

Thanks in advance,
Eran


Comment
Watch Question
Commented:
This problem has been solved!
Unlock 3 Answers and 3 Comments.
See Answers
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE