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.

Calling Windows API from C#

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
sonu - 21 Aug 2003 12:56 GMT
Hi,

I am calling "GetPrivateProfileString" function to
retrieve some key/value from my ini file in C# code.

I have used the DllImport Method to import the function
from Kernel32.dll.

For some reason, when i call this, it does not seem to
work. In the same scenario , another API function from the
same dll called "MoveFile" works. I am putting my code
below. Please advise.

namespace Localization
{
class ReadINI
{
[DllImport
("kernel32",EntryPoint="GetPrivateProfileString",ExactSpell
ing=false, CharSet=CharSet.Auto,SetLastError=true)]
public static extern long GetPrivateProfileString(string
Section, string Key, string Default, string KeyValue ,long
Size , string INIFileName);
[DllImport
("kernel32",EntryPoint="MoveFile",ExactSpelling=false,
CharSet=CharSet.Auto,SetLastError=true)]
    public static extern bool MoveFile(string srcfile,
string destfile);   
        }

When I try to call it from Main() it looks like this.

static void Main()
    {
    long x;
    string sKeyvalue;           
    string Ldir;
    long xval;
    xval=0;
    Ldir="C:\\windows\\Localisation.ini";
    sKeyvalue="";
x = ReadINI.GetPrivateProfileString
("English","001","NotFound",sKeyvalue,xval,Ldir);

    }

What could be the problem ??

Regards
Sonu
Mattias Sj?gren - 21 Aug 2003 14:59 GMT
> [DllImport
>("kernel32",EntryPoint="GetPrivateProfileString",ExactSpell
>ing=false, CharSet=CharSet.Auto,SetLastError=true)]
> public static extern long GetPrivateProfileString(string
>Section, string Key, string Default, string KeyValue ,long
>Size , string INIFileName);

The return type and the type of the Size parameter should be (u)int,
not long. The KeyValue parameter should be a StringBuilder, not a
string.

Mattias

Signature

Mattias Sjögren [MVP]  mattias @ mvps.org
http://www.msjogren.net/dotnet/
Please reply only to the newsgroup.

Sonu - 26 Aug 2003 11:55 GMT
Hi Mattias,

Thanx a lot. :) U did solve my problem.

Regards
Sonu
>-----Original Message-----
>
>> [DllImport

("kernel32",EntryPoint="GetPrivateProfileString",ExactSpell
>>ingúlse, CharSet=CharSet.Auto,SetLastError=true)]
>> public static extern long GetPrivateProfileString(string
[quoted text clipped - 6 lines]
>
>Mattias

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.