Results 1 to 9 of 9

Thread: DllImport question

  1. #1

    Thread Starter
    l33t! MrPolite's Avatar
    Join Date
    Sep 2001
    Posts
    4,428

    DllImport question

    There's one thing I'm not quite understanding. I'm looking at this page on MSDN and the DllImport syntax is confusing me:
    [DllImport("KERNEL32.DLL", EntryPoint="MoveFileW", SetLastError=true,
    CharSet=CharSet.Unicode, ExactSpelling=true,
    CallingConvention=CallingConvention.StdCall)]
    public static extern bool MoveFile(String src, String dst);


    when they use EntryPoint, what does entryPoint refer to ? is it an application wide variable? why doesn't the app compile if I remove the "entrypoint=" part? it's a bit confusing
    rate my posts if they help ya!
    Extract thumbnail without reading the whole image file: (C# - VB)
    Apply texture to bitmaps: (C# - VB)
    Extended console library: (VB)
    Save JPEG with a certain quality (image compression): (C# - VB )
    VB.NET to C# conversion tips!!

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: DllImport question

    There is a MoveFile function and a MoveFileW function in that library, which is why it still compiles without it. MoveFileW is the Unicode implemetation. The EntryPoint is the actual library function that gets called, while the in the declaration is the name you will use in code. You only need to use EntryPoint if the declared name and the actual name are different.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    l33t! MrPolite's Avatar
    Join Date
    Sep 2001
    Posts
    4,428

    Re: DllImport question

    oh umm thanks
    I'm not actually concerned about that particular function. What I don't understand is that it looks like "entryPoint" is a variable and a string is being assigned to it (look at the red part?). The other parameters are like this also.
    I mean why cant I do dllImport ("kernel32.dll", "moveFileW", true, ...)
    why is it assigning things to variables? I don't have those "variables" defined anywhere in my app either, but still it compiles
    rate my posts if they help ya!
    Extract thumbnail without reading the whole image file: (C# - VB)
    Apply texture to bitmaps: (C# - VB)
    Extended console library: (VB)
    Save JPEG with a certain quality (image compression): (C# - VB )
    VB.NET to C# conversion tips!!

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: DllImport question

    What you are doing is setting the EntryPoint field of the DllImportAttribute object being applied to the library function you are declaring. Have a look in the help or on MSDN for the DllImportAttribute class and its members.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5
    Frenzied Member Mike Hildner's Avatar
    Join Date
    Jul 2002
    Location
    Des Moines, NM
    Posts
    1,690

    Re: DllImport question

    I think I get what you're asking, MrPolite. The reason it works like that is because DllImport is an attribute. Attributes have their own convention, so "EntrypPoint" has nothing to do with a variable in your code, but everything to do with the DllImportAttribute.

    I'm not qualified to give you all the details, but it's explained quite well in the book Inside C#, Second Edition (and probably other places as well).

    Just for kicks, check out the MSIL that's produced from your attribute:
    Code:
    .method public hidebysig static pinvokeimpl("KERNEL32.DLL" as "MoveFileW" nomangle unicode lasterr) 
            bool  MoveFile(string src,
                           string dst) cil managed preservesig
    {
    }
    You can see that the compiler uses the tags, which are defined by the attribute, to create the MSIL.

  6. #6

    Thread Starter
    l33t! MrPolite's Avatar
    Join Date
    Sep 2001
    Posts
    4,428

    Re: DllImport question

    aha interesting. thanks guys
    I'm not so used to these attributes so they didn't make too much sense to me
    rate my posts if they help ya!
    Extract thumbnail without reading the whole image file: (C# - VB)
    Apply texture to bitmaps: (C# - VB)
    Extended console library: (VB)
    Save JPEG with a certain quality (image compression): (C# - VB )
    VB.NET to C# conversion tips!!

  7. #7
    type Woss is new Grumpy; wossname's Avatar
    Join Date
    Aug 2002
    Location
    #!/bin/bash
    Posts
    5,682

    Re: DllImport question

    Attributes are really weird. I need to read up on them. Its like half way between precompiler arguments and normal code.
    I don't live here any more.

  8. #8

    Thread Starter
    l33t! MrPolite's Avatar
    Join Date
    Sep 2001
    Posts
    4,428

    Re: DllImport question

    aha interesting
    do these stuff exist in C++ also (not the .NET version), or is the idea new to .NET?
    rate my posts if they help ya!
    Extract thumbnail without reading the whole image file: (C# - VB)
    Apply texture to bitmaps: (C# - VB)
    Extended console library: (VB)
    Save JPEG with a certain quality (image compression): (C# - VB )
    VB.NET to C# conversion tips!!

  9. #9
    Frenzied Member Mike Hildner's Avatar
    Join Date
    Jul 2002
    Location
    Des Moines, NM
    Posts
    1,690

    Re: DllImport question

    Quote Originally Posted by MrPolite
    do these stuff exist in C++ also (not the .NET version), or is the idea new to .NET?
    From what I've read (not from a deep understanding of many programming languages), attributes are a new feature in .NET. "Nothing short of groundbreaking" says one.

    Again I'd have to refer you to Inside C#. "Chapter 6 Attributes" has the most excellent explanation of System.Attribute I've seen, better than you can find on MSDN.

    If you don't want to buy the book, at least get a latte and read chapter 6

    Mike

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width