Jump to content

Fetch architecture to load libraries the same way KSP_x64 does


Recommended Posts

Hi,

I'm in the uncomfortable situation that I need to load an external library for a mod, that exists in an x86 and an x86_64 DLL. I noticed, that KSP uses the .exe filename to figure out which data folder to use. Is there any way for me to do the same to load the correct dll? My problem is, that the DLLImport code asks for a const string and I can't alter/fill that at runtime...

Link to comment
Share on other sites

A nice trick to determine if you are running on 32 or 64 bit would be to evaluate the size in byte of a pointer. Luckily enough, that is also a constant.

[DllImport(IntPtr.Size == 4 ? "my_32bit.dll" : "my_64bit.dll")]
public static extern int my_dll_function(string something, uint something_else);

 

Edited by ShotgunNinja
Link to comment
Share on other sites

3 minutes ago, ShotgunNinja said:

A nice trick to determine if you are running on 32 or 64 bit would be to evaluate the size in byte of a pointer. Luckily enough, that is also a compile time constant.


[DllImport(IntPtr.Size == 4 ? "my_32bit.dll" : "my_64bit.dll")]
public static extern int my_dll_function(String something, uint something_else);

 

That doesn't work, I'm getting the following error: An attribute argument must be a constant expression

Is there a way around that?

Link to comment
Share on other sites

mmm...

Try this

[DllImport("my_32bit.dll")]
public static extern int my_dll_function32(string a, uint b);

[DllImport("my_64bit.dll")]
public static extern int my_dll_function64(string a, uint b);

public static int my_dll_function(string a, uint b)
{
  if (IntPtr.Size == 4) return my_dll_function32(a, b);
  else return my_dll_function64(a, b);
}

The DLL is loaded at the first call at run-time, so only the right one will end up being imported in the virtual memory of the process.

Edited by ShotgunNinja
Link to comment
Share on other sites

5 minutes ago, ShotgunNinja said:

mmm...

Try this


[DllImport("my_32bit.dll")]
public static extern int my_dll_function32(string a, uint b);

[DllImport("my_64bit.dll")]
public static extern int my_dll_function64(string a, uint b);

public static int my_dll_function(string a, uint b)
{
  if (IntPtr.Size == 4) return my_dll_function32(a, b);
  else return my_dll_function64(a, b);
}

The DLL is loaded at the first call at run-time, so only the right one will end up being imported in the virtual memory of the process.

You sir are a genius. *tipshat*

Link to comment
Share on other sites

7 hours ago, stupid_chris said:

to avoid checking each time you must call the function , i suggest instead assigning it to a delegate once in a static constructor then calling said delegate.

Totally this.

 

And IntPtr.Size is not a constant...

 

Link to comment
Share on other sites

This thread is quite old. Please consider starting a new thread rather than reviving this one.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...