none
Late Binding a dll RRS feed

  • Question

  • Yes.... I know.. there are about a bazillion articles out there telling me how to do this. The thing is, I cant quite make the examples fit my situation. It's like watching the professor explain a problem on the white board and thinking... I can do this. Then getting home and trying it with variables that don't match the example and being completely baffled about how it can be so easy when he does and so confusing when I do it.
    BACKGROUND:

    The company I work for does credit card processing. We have software that just about always works perfectly sometimes. As a tech, my job is to troubleshoot it when it doesn't work. One small peice of the puzzle is a dll called NetSend. This is one of many pieces that I have to test when I'm troubleshooting why "the credit cards are broke". Since I'm a shade tree developer, I decided to write a program that will test all the different pieces of this puzzle. This works great! I added a reference to the dll, called the functions that I needed to call and got the results I was looking for.
    TIME FOR SOME TESTING:

    So I got everything working, set up a test server to mimik a client install, loaded my tester, and got a great big FAIL on the NetSend test. I did some digging around and found out that there are at least 2 versions (maybe more) of the NetSend dll. Version 1.0.2.1 and version 3.2.0.0 Awesome. And since I compliled my exicuteble with the entry points for version 1, and was trying to test version 3, I was referencing the wrong memory addresses for the function calls.
    TIME FOR SOME GOOGLE:

    So I jumped on google for the answer. I found tons of links and good code and great explinations. And none of them made any sence. I eventually came accross an article on "Late Binding". Yes! this is what I'm looking for. Insert more Google's. Again, none of the samples fit what I need to do.

    Here is the code that works with "Early Binding":

    I've added a reference to the NetSend dll

    // getting the year and adding 2 to it for the exp date on the 
    
    // credit card.. exp dates will never expire with this method
    
    DateTime y = DateTime.Now.Date.AddYears(2);
    
       
    
    // create a NetSend Object
    
    NetSend NS = new NetSend();
    
       
    
    // add the basic API fields
    
       
    
    NS.SetNewField("1", "1");
    
    NS.SetNewField("2", "1.00");
    
    
    
    // cant store card numbers
    
    NS.SetNewField("3", txtCardNumber.Text);
    
    NS.SetNewField("4", "12" + y.ToString("yy"));
    
    NS.SetNewField("7", "123456");
    
    NS.SetNewField("105", ".\\");
    
    NS.SetNewField("106", ".\\");
    
    NS.SetNewField("109", txtTermID.Text);
    
    NS.SetNewField("110", "Test");
    
    
    
    NS.SendInputFile("1");
    
       
    
    // call the parser to clean the response for display
    
    txtResponse.Text += "\n\r" + Parser.ParseNetSend(NS.GetStringOutput()) + "\n\r";
    
    

    So I have 3 functions that need to be called:

    SetNewField -- that takes two string variables.

    SendInputFile -- that takes one string variable.

    GetStringOutput -- that returns a string variable.

    My hope is that since both versions of the dll work when I add a reference to them and compile the exe (early binding) that when I Late Bind them it will work regardless of the version. The function calls are the same, just with different memory addresses.

    Please, if anyone can help me with this, and explain it so the derp in me can understand, I would be verry greatfull.

    regards

    Jim


    • Moved by Cookie Luo Friday, June 3, 2011 7:31 AM (From:Visual C# Language)
    Wednesday, June 1, 2011 11:05 PM

Answers

  • OK. I just got it working. Not sure how, but I added the following line of code:

    [

    DllImport(@"c:\Windows\System32\NetSend.dll", EntryPoint = "NetSend.cNetSend"

    )]

     

    public static extern NetSend.cNetSend SetNewField(string name, string value);

    Don't ask me how or why, because I have no idea. But once I added the line above, everything started working. I tested it on the older version of the dll and got a valid result. I switched to the newer version of the dll and got the same result. Then, I removed the path and tried it. Same good result.

    [

     

    DllImport("NetSend.dll", EntryPoint = "NetSend.cNetSend"

    )]

     

     

    public static extern NetSend.cNetSend SetNewField(string name, string value);

    Doing it this way will allow it to work when I'm testing the dll on a 64bit OS since the dll will be in the WOW64 directory and not the system32 diretory.

     

     
     
    • Marked as answer by Paul Zhou Monday, June 6, 2011 6:34 AM
    Thursday, June 2, 2011 2:14 PM

All replies

  • Hi Jim,

    So you must be using reflection to late bind the dll? or are you using DLLImport?

    Can you share the code you are using for late binding as well?


    Please mark this as answer or vote as helpful if it solved your problem
    Thursday, June 2, 2011 7:23 AM
  • Thats the thing. I'm not sure what approach to use. I have to be able to call the "SedNewField" function over and over, and the other two at least once. Which is ever way is the most flexible is the way that I would like to do it.
    Thursday, June 2, 2011 1:38 PM
  • OK. I just got it working. Not sure how, but I added the following line of code:

    [

    DllImport(@"c:\Windows\System32\NetSend.dll", EntryPoint = "NetSend.cNetSend"

    )]

     

    public static extern NetSend.cNetSend SetNewField(string name, string value);

    Don't ask me how or why, because I have no idea. But once I added the line above, everything started working. I tested it on the older version of the dll and got a valid result. I switched to the newer version of the dll and got the same result. Then, I removed the path and tried it. Same good result.

    [

     

    DllImport("NetSend.dll", EntryPoint = "NetSend.cNetSend"

    )]

     

     

    public static extern NetSend.cNetSend SetNewField(string name, string value);

    Doing it this way will allow it to work when I'm testing the dll on a 64bit OS since the dll will be in the WOW64 directory and not the system32 diretory.

     

     
     
    • Marked as answer by Paul Zhou Monday, June 6, 2011 6:34 AM
    Thursday, June 2, 2011 2:14 PM
  •  

    Hi Jim,

     

    For DllImport and EntryPoint attribute, please refer to:

    Platform Invoke Tutorial

    DllImportAttribute.EntryPoint Field


    Paul Zhou [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Monday, June 6, 2011 6:34 AM