4
votes

I want to use a C++CLI (Managed C++) .dll to interface between Powershell v3 x86 and a (Unmanaged) C++ application.

I've tried to load a very simple "Hello World" program written in C++CLI using both Add-Type and [System.Reflection.Assembly]::LoadWithPartialName() in powershell but I get the error: "Could not load file or assembly"

I then wrote a C# wrapper for the C++CLI class, successfully loaded it into Powershell using the same method, but when I make a call that references the C++CLI code I get the same error. The C# code itself seems to not take any issue with calls to the C++CLI .dll, but Powershell does.

Accessor.h:

using namespace System;

namespace Accessor {

    static public ref class Greeting
    {
  public:
    static void HelloWorld();
    };
}

Accessor.cpp:

#include "Accessor.h"

void Accessor::Greeting::HelloWorld()
{
  Console::WriteLine("Hello World!\n");
}

Wrapper.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Accessor;

namespace AccessWrap
{
    static public class Wrapper
    {
      static public void HelloWorld()
      {
        Greeting.HelloWorld();
      }
    }
}

What is the correct way to accomplish this? I've tried many other methods I've found online but none have worked for me.

Edit: This is the exact error I get when trying to load the C++CLI .dll directly: http://imgur.com/HF5VRkf

1
Could it be related to the C++ runtime not available on the machine where you are testing?David Brabant
What is the exact error you get? The message sounds like a FileLoadException, which usually has details on why the assembly can't be loaded.Jeroen Mostert
I edited my post with a link to a picture of the error message, my name is omitted from the file path.johann_dev
Please don't post screenshots for issues that aren't actually related to graphics, take the time to type things out in the question.Jeroen Mostert
"An attempt was made..." is COR_E_BADIMAGEFORMAT, which usually (but not always) indicates that a 32-bit process is attempting to load a 64-bit DLL or vice versa. You'll have to investigate that possibility first.Jeroen Mostert

1 Answers

0
votes

I was accidentally using 64 bit powershell, not x86 like I thought. I switched to x86 and it works fine.

Thanks for everyone's help.