Visual c++ Pinvoke和本机呼叫的Hello World

Visual c++ Pinvoke和本机呼叫的Hello World,visual-c++,win-universal-app,Visual C++,Win Universal App,我正在尝试为Pinvoke和本地电话创建一个非常基本的hello world。 我创建了一个包含2个项目的解决方案(一个用于dll,一个用于universal windows应用程序) 所以我最终得到了一个像这样的项目继承人 我的dll(文件NativeCalls.cpp)中有一个方法: 在这一点上,我将建立和运行它,但得到一个错误,它无法找到dll 然而,我相信这是依赖性,而不是dll本身。我已将dll的输出目录更改为运行UW应用程序的根目录(\bin\x86),因此它确实应该找到它。就

我正在尝试为Pinvoke和本地电话创建一个非常基本的hello world。
我创建了一个包含2个项目的解决方案(一个用于dll,一个用于universal windows应用程序)

所以我最终得到了一个像这样的项目继承人

我的dll(文件NativeCalls.cpp)中有一个方法:

在这一点上,我将建立和运行它,但得到一个错误,它无法找到dll

然而,我相信这是依赖性,而不是dll本身。我已将dll的输出目录更改为运行UW应用程序的根目录(\bin\x86),因此它确实应该找到它。就像我说的,我认为是依赖关系,而不是实际的dll

这是我在Dependency Walker中看到的 但是我已经安装了所有我能掌握的C++包,所以我不知道如何获得丢失的依赖项。加上这只是一个你好的世界,为什么我需要所有这些库

供参考 UW应用程序未引用我的dll项目。我不确定这是否需要?我不这么认为,因为这是一个运行时的东西,所以只要dll在那里,它应该找到它并读取它。但无论我是否尝试添加项目作为参考,我都会得到以下错误:
对我最大的帮助是找到这些方法声明(甚至在类中也没有)

#include "pch.h"
#include "NativeCalls.h"
#include <stdio.h>

MYAPI void print_line(const char* str) {
    printf("%s\n", str);
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;



namespace MSSurfaceHubMonitoring
{

    public static class NativeCalls
    {

        [DllImport("NativeCalls.dll")]
        private static extern void print_line(string str);

        public static void sayHelo()
        {
            print_line("Hello, PInvoke!");
        }
    }
}
extern "C" {
    __declspec(dllexport) int getPageSize()
    {
        SYSTEM_INFO siSysInfo;
        GetSystemInfo(&siSysInfo);
        return siSysInfo.dwPageSize;
    }
}

extern "C" {
    __declspec(dllexport) Windows::Foundation::Collections::IMap<Platform::String^, int> ^getSystemInfo()
    {
        SYSTEM_INFO siSysInfo;
        GetSystemInfo(&siSysInfo);

        IMap<String^, int> ^ret =
            ref new Platform::Collections::Map<String^, int>;
        ret->Insert("oemId", siSysInfo.dwOemId);
        ret->Insert("cpuCount", siSysInfo.dwNumberOfProcessors);
        ret->Insert("pageSize", siSysInfo.dwPageSize);
        ret->Insert("processorType", siSysInfo.dwProcessorType);
        ret->Insert("maxApplicationAddress", siSysInfo.lpMinimumApplicationAddress);
        ret->Insert("minApplicationAddress", siSysInfo.lpMaximumApplicationAddress);
        ret->Insert("activeProcessorMask", siSysInfo.dwActiveProcessorMask);
        return ret;
    }
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;


namespace Monitoring
{

    public static class NativeCallsWrapper
    {
        private static SYSTEM_INFO sysInfo = new SYSTEM_INFO();
        private static MEMORYSTATUSEX mem = new MEMORYSTATUSEX();

        [DllImport("kernel32.dll", SetLastError = false)]
        public static extern void GetSystemInfo([In, Out] SYSTEM_INFO Info);

        [return: MarshalAs(UnmanagedType.Bool)]
        [DllImport("kernel32.dll", SetLastError = true)]
        static extern bool GlobalMemoryStatusEx([In, Out] MEMORYSTATUSEX lpBuffer);

        static NativeCallsWrapper()
        {
            GetSystemInfo(sysInfo);
            GlobalMemoryStatusEx(mem);
        }

        [StructLayout(LayoutKind.Explicit)]
        public struct SYSTEM_INFO_UNION

        {

            [FieldOffset(0)]
            public UInt32 OemId;
            [FieldOffset(0)]
            public UInt16 ProcessorArchitecture;
            [FieldOffset(2)]
            public UInt16 Reserved;
        }

        public struct SYSTEM_INFO

        {

            public SYSTEM_INFO_UNION CpuInfo;
            public UInt32 PageSize;
            public UInt32 MinimumApplicationAddress;
            public UInt32 MaximumApplicationAddress;
            public UInt32 ActiveProcessorMask;
            public UInt32 NumberOfProcessors;
            public UInt32 ProcessorType;
            public UInt32 AllocationGranularity;
            public UInt16 ProcessorLevel;
            public UInt16 ProcessorRevision;
        }

        [StructLayout(LayoutKind.Sequential)]
        public class MEMORYSTATUSEX
        {
            public uint dwLength;
            public uint dwMemoryLoad;
            public ulong ullTotalPhys;
            public ulong ullAvailPhys;
            public ulong ullTotalPageFile;
            public ulong ullAvailPageFile;
            public ulong ullTotalVirtual;
            public ulong ullAvailVirtual;
            public ulong ullAvailExtendedVirtual;
            public MEMORYSTATUSEX()
            {
                this.dwLength = (uint)Marshal.SizeOf(typeof(MEMORYSTATUSEX));
            }
        }

        public static GeneralStatistics getGeneralStatistics()
        {
            GeneralStatistics generalStatistics = new GeneralStatistics();
            generalStatistics.numberOfProcesses = (int)sysInfo.NumberOfProcessors;
            generalStatistics.memoryTotal = mem.ullTotalPhys / 1048;
            generalStatistics.memoryInUse = (mem.ullTotalPhys - mem.ullAvailPhys) / 1048;
            return generalStatistics;
        }
    }
}