DllImport

Discussion in 'MCAD' started by Marek, May 1, 2006.

  1. Marek

    Marek Guest

    I found in one smart book one simple question... how to use DLLIMPORT
    (user32.dll and CallMe method...

    Two answers:

    a) [DllImport("user32.dll")]
    publi .....


    b) [DllImport("user32.dll"), EntryPoint="CallMe")]
    publi....


    In my opinion answer A is correct, but in this smart book correct
    answer is B.

    Why?
     
    Marek, May 1, 2006
    #1
    1. Advertisements

  2. Marek

    Marek Guest

    Question:

    You need to call the function, CallMe(), located in the user32.dll
    library.
    The signature of the function is as follows:
    string CallMe (string Name, string Address, string Phone
    Which code segment will make the function available to your
    application?

    Full list of answers:

    A. [DllImport("CallMe.dll")]
    public static extern string CallMe (string Name, string
    Address, string Phone)];
    B. [[DllImport("CallMe.dll", EntryPoint="CallMe")]
    public static extern string CallMe (string Name, string
    Address, string Phone)];
    C. [DllImport("user32.dll", EntryPoint="CallMe")]
    public static extern string CallMe (string Name, string
    Address, string Phone)];
    D. [DllImport("user32.dll")]
    public static extern string CallMe (string Name, string
    Address, string Phone)];
     
    Marek, May 1, 2006
    #2
    1. Advertisements

  3. Marek

    Cerebrus Guest

    C is correct.
     
    Cerebrus, May 1, 2006
    #3
  4. Marek

    Marek Guest

    Why not A?
     
    Marek, May 1, 2006
    #4
  5. Marek

    Simon Hart Guest

    Because the procedure is located in user32.dll not CallMe.dll - according to
    the question.

    Lookup "platform invokes or p/invokes" on MSDN.

    Simon.
     
    Simon Hart, May 1, 2006
    #5
  6. Marek

    Marek Guest

    Not A, sorry!

    I'm thinking about answer D. What is wrong with D?
     
    Marek, May 2, 2006
    #6
  7. Marek

    Cerebrus Guest

    D could have been possible, but C is definitely the better answer since
    it tells the compiler which function to use as entry point. Also, it is
    more useful if you want to rename the function.
     
    Cerebrus, May 2, 2006
    #7
  8. D could have been possible, but C is definitely the better answer since
    I disagree, since it makes the code harder to maintain.
    In my exp saying the same thing twice makes the code harder to reuse, change
    and read.

    BTW. What is the function of the last closing bracket?

    <snippety>
    [DllImport("user32.dll")]
    public static extern string CallMe (string Name, string
    Address, string Phone)];
    </snipp>
     
    Gorm Braarvig, May 4, 2006
    #8
  9. Marek

    Simon Hart Guest

    How does it make it harder to reuse, change and read?

    Sometimes you want to use the entrypoint clause as you might want to name
    the method something more meaningful to your application. Or you might want
    to make it more unstandable as some old legacy unmanaged API calls are not
    very intutive.

    I think the final square bracket is a typo.

    Simon.

     
    Simon Hart, May 8, 2006
    #9
  10. Greetings!
    comments in-line.
    When copying some working code and creating small changes, I prefer to have
    as few points as possible to change, so that it is harder to mess things up.
    This may or may not be a personal preference/problem.
    Thats why I keep these methods private or have them in an embedded
    (protected) class. I keep the embedding class small to.

    I disagree that saying the same thing twice (using entrypoint clause with
    the same name as the method) is a better strategy than being less chatty. In
    the case of "old legacy unmanaged API calls" it depends on the
    documentation:
    - I think WINAPI is better not renamed (and used without entrypoint clause)
    because of MSDN.
    - I think some old API calls might be renamed, in that case I would prefer
    not to use entrypoint class in the methods not renamed (so that the ones
    renamed stands out of the crowd).

    BTW: "CallMe(), located in the user32.dll" is very confusing.
    In the case of "CallMeIfYouDare(), located in the my_strange_dll.dll" I
    might agree with you.
    In the case of "MessageBox, located in the user32.dll" would be easier to
    answer.

    And yes, I think MsgBox is a silly idea. I think MsgBox should be a public
    method on a class containing a private method ShowMessage.

    Of course, there might be smarter people than me with other opinions than
    me.

    Gorm
     
    Gorm Braarvig, May 8, 2006
    #10
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.
Similar Threads
There are no similar threads yet.
Loading...