0

Good day

I was looking for a way to implement the below code, inline; As in doing the implementation just before I call the code

[DllImport(dllName: "utilities.dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi)]
public static extern string EncryptCardNumber(string c);

The utilities.dll is not releasing its memory and I am looking for a way around that by only using it "inline".

I found calling GC.Collect() just after calling EncryptCardNumber was working but was slowing the program for a 9min run till above an hour.

I know the below DLLImport has to be outside the function it just to show what I hope to achieve

 private string doWork(string a)
 {
  //need to replace below with "inline" import  
  [DllImport(dllName: "utilities.dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi)]

  return EncryptCardNumber(a);
 }
JvD
  • 473
  • 3
  • 18
  • If I understand correctly, you're trying to get the DLL to load only when you call doWork. I'm fairly sure the DLL is only loaded once the class is loaded containing the `DllImport`. Try putting the `DllImport` static function in a separate static class. And only use this class from your doWork method. For unloading, see https://stackoverflow.com/questions/2445536/unload-a-dll-loaded-using-dllimport – ikkentim Sep 06 '18 at 13:22
  • Unless someone comes along with an official way to do it, I suppose you could try something like `while(FreeLibrary(GetModuleHandle('utilities.dll')));`. (These functions are both native, and must be imported from kernel32.dll.) – 500 - Internal Server Error Sep 06 '18 at 13:22
  • Thanks. I'll look into it. probably going to have to contact the dll supplier. was hoping for an easy fix. – JvD Sep 06 '18 at 13:34
  • Sounds like you want to use *late binding*. Search for that word, e.g. [here](https://stackoverflow.com/q/36933864/1997232) is something, but unfortunately its without `dynamic` (which makes code simpler). – Sinatr Sep 06 '18 at 13:44
  • 2
    The DLL is indeed only loaded when you first call it. I'm pretty sure that you have completely misunderstood what is actually happening, and have incorrectly diagnosed the problem. Before you are able to solve this problem you will need to understand it properly. Don't attempt to solve it until you do. – David Heffernan Sep 06 '18 at 13:55

0 Answers0