avoid P/INVOKE or COM Interop if possible

Published 02-09-2006 6:09 PM | jokiz

i was debugging this activex control used in my current project when i bumped into this wrapper class for an interop with kernel32.dll methods. after browsing the documentation, it says that the following functions are provided for win16 support. how come the developer used the following functions? maybe he is trying to reuse his code that he got from a vb6 project.


[DllImport("KERNEL32.DLL",  EntryPoint="GetPrivateProfileSection")]

protected internal static extern int

    GetPrivateProfileSection(string lpAppName, string lpReturnedString ,long nSize,string lpFileName);

 

[DllImport("KERNEL32.DLL",  EntryPoint="GetPrivateProfileString")]

protected internal static extern int

    GetPrivateProfileString(string lpApplicationName, string lpKeyName, string lpDefault, StringBuilder lpReturnedString, int nSize, string lpFileName);

 

[DllImport("KERNEL32.DLL",  EntryPoint="WritePrivateProfileSection")]

protected internal static extern int

    WritePrivateProfileSection(string lpAppName, string lpString, string lpFileName);

 

[DllImport("KERNEL32.DLL",  EntryPoint="WritePrivateProfileString")]

protected internal static extern int WritePrivateProfileString(string lpApplicationName, string lpKeyName, string lpString, string lpFileName);

 


in .NET, you have to avoid COM interop and PINVOKEs as much as possible. MS tried to provide a .NET interface to most of the windows API and it is seldom that you will need to resort to these kind of method calls. As I found out, the above functions are used just to read and write files. .NET is now using configuration files for an application configuration and they have provided the System.Configuration.ConfigurationSettings class for this.

Comments

# cruizer said on February 9, 2006 8:41 PM:

i have my own reason for avoiding P/Invoke and COM interop: I'd like my app to be cross-platform someday :D

# keithrull said on February 9, 2006 9:18 PM:

INI Files! Yikes!

# Christopher said on October 31, 2006 11:07 PM:

Weird enough for government work.

# Jacob said on November 22, 2006 10:48 PM:

...and that is how we know the Earth to be banana-shaped.

# Victor@ru said on June 14, 2007 5:01 AM:

> ...kernel32.dll methods... PrivateProfile...

> the above functions are used just to read and write

> files.

Yes. The INI files.

We need this methods to support current applications.

New applications does not require the INI files.

> .NET is now using configuration files for an

> application configuration and they have provided the

> System.Configuration.ConfigurationSettings class for

> this.

The ConfigurationSettings class operates with the XML files. It's fine for applications... without backward compatibility.