Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / Interop / August 2003

Tip: Looking for answers? Try searching our database.

PInvoke and GetPrivateProfileString

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Daniel F. Devine - 28 Aug 2003 19:52 GMT
Hi -

The following snippet of code:

class Class1
{

 [DllImport("kernel32")]
 public static extern ulong
GetPrivateProfileString([MarshalAs(UnmanagedType.LPStr)]string app,

[MarshalAs(UnmanagedType.LPStr)]string key,

[MarshalAs(UnmanagedType.LPStr)]string def,

StringBuilder retval,

ulong size,

[MarshalAs(UnmanagedType.LPStr)]string file);
 [DllImport("kernel32")]
       public static extern ulong GetLastError();

 [STAThread]
 static void Main(string[] args)
 {
  string appname = "LOGON";
  string key     = "USER";
  string df      = "Helpless";
   string fn      = @"C:Work\ReadINI\ReadIni\bin\varisol.ini";

  StringBuilder sb = new StringBuilder(20);

  ulong errorcode = 0UL;

  errorcode = GetPrivateProfileString(appname,key,df,sb,21UL,fn);

  errorcode = GetLastError();

 }
}

is intended to read the user name from the logon section of an ini file.
The StringBuilder variable contains the default value of  "Helpless" after
invoking the function - so that it appears the function was at least
invoked.  However, the correct value is never returned and the last error
function indicates that the specified function was not found.  Now, I don't
know if it is even valid to call GetLastError through interop but
GetPrivateProfileString doesn't return an error code - so I have resorted to
this.

Does anybody know what I am doing wrong ?

Thanks for any help

The ini file looks like this:

[LOGON]
USER=Bill
Vadim Melnik - 28 Aug 2003 21:18 GMT
Hi,

> [MarshalAs(UnmanagedType.LPStr)]string file);
>   [DllImport("kernel32")]
>         public static extern ulong GetLastError();
>
>  Now, I don't
> know if it is even valid to call GetLastError through interop

No, it's not valid. See Marshal.GetLastWin32Error  method to retrieve Win32
error code (note unmanaged function called via P/Invoke should have
DllImportAttribute.SetLastError field set to true).

..
Regards,
Vadim.
Daniel F. Devine - 28 Aug 2003 22:55 GMT
> Hi,
>
[quoted text clipped - 12 lines]
> Regards,
> Vadim.

Vadim -

Thanks for the reply - I finally got the function to work by changing the
size paramater from an unsigned long to an int.  The GetPrivateProfileString
Docs state that this is a DWORD and the Framework Docs state that a DWORD is
a ulong - so I don't know why this was failing.  Does anyone have any
insight - are the Docs wrong?  I mean that seems unlikely .
Vadim Melnik - 29 Aug 2003 08:51 GMT
Hi,

> Thanks for the reply - I finally got the function to work by changing the
> size paramater from an unsigned long to an int.  The GetPrivateProfileString
> Docs state that this is a DWORD and the Framework Docs state that a DWORD is
> a ulong - so I don't know why this was failing.  Does anyone have any
> insight - are the Docs wrong?  I mean that seems unlikely .

As far as I know System.UInt32 or System.Int32 are valid .NET data types for
Win32 DWORD type (more exactly only first variant is valid) . There is no
error in documentation [1], "unsigned long" is C equivalent, and
System.UInt32 mentioned as managed variant.

Your code failed because ulong in C# is unsigned 64-bit integer
(System.UInt64), while GetPrivateProfileString accepts 32-bit parameter.
Hope this explanation helps somehow.

[1]
http://msdn.microsoft.com/library/en-us/cpguide/html/cpconplatforminvokedatatypes.asp

..
Regards,
Vadim.
Daniel F. Devine - 29 Aug 2003 14:20 GMT
> Hi,
>
[quoted text clipped - 16 lines]
>
> [1]

http://msdn.microsoft.com/library/en-us/cpguide/html/cpconplatforminvokedatatypes.asp

> ..
> Regards,
> Vadim.

Yes it does - thank you very much - I didn't realize the C# ulong was 64
bits and not 32bits.  Thanks for the explanation.

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.