4

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: https://i.stack.imgur.com/ECou2.jpg

johann_dev
  • 43
  • 4
  • Could it be related to the C++ runtime not available on the machine where you are testing? – David Brabant Jun 23 '16 at 07:21
  • 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 Jun 23 '16 at 08:36
  • I edited my post with a link to a picture of the error message, my name is omitted from the file path. – johann_dev Jun 23 '16 at 16:46
  • 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 Jun 24 '16 at 12:54
  • 1
    "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](http://stackoverflow.com/questions/2023766/) first. – Jeroen Mostert Jun 24 '16 at 12:55

1 Answers1

0

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.

johann_dev
  • 43
  • 4