tjanczuk / edge

Run .NET and Node.js code in-process on Windows, MacOS, and Linux
http://tjanczuk.github.io/edge
Other
5.41k stars 641 forks source link

Issue including Windows.Networking.Proximity #396

Closed sgithens closed 1 month ago

sgithens commented 8 years ago

Hi,

I'm using edge on a Windows 10 box with Node 4.3.1. I've tried this with both the 32-bit and 64-bit versions of Node.

For some reason, I can't seem to include the Window.Networking.Proximity assemblies. On both versions of Node I've also tried including the Windows.Networking.Proximity.dll from both Windows/System32 and Windows/SysWOW64. Each time I get the same error. Any suggestions would be greatly appreciated.

Thanks much! Steve

test.js

var edge = require('edge');
var path = require('path');

var helloWorld = edge.func({
    source: path.join(__dirname, 'testlistener.cs'),
});

helloWorld('JavaScript', function (error, result) {
    if (error) throw error;
    console.log(result);
});

testlistener.cs

#r "C:\Windows\System32\Windows.Networking.Proximity.dll"
using Windows;
// using Windows.Networking.Proximity;

async (input) => {
    return ".NET debugging" + input.ToString();
}

output

 PS C:\Users\LGS-User\code\edgetest> node test.js

C:\Users\LGS-User\code\edgetest\node_modules\edge\lib\edge.js:161
    return edge.initializeClrFunc(options);
                ^
 Error: Unable to compile C# code.
----> Errors when compiling as a CLR library:
c:\Users\LGS-User\AppData\Local\Temp\1askhx31.0.cs(2,1) : error CS0116: A namespace cannot directly contain members such
 as fields or methods
----> Errors when compiling as a CLR async lambda expression:
error CS0009: Metadata file 'c:\Windows\System32\Windows.Networking.Proximity.dll' could not be opened -- 'An attempt wa
s made to load a program with an incorrect format. '
    at Error (native)
    at Object.exports.func (C:\Users\LGS-User\code\edgetest\node_modules\edge\lib\edge.js:161:17)
    at Object.<anonymous> (C:\Users\LGS-User\code\edgetest\test.js:4:23)
    at Module._compile (module.js:409:26)
    at Object.Module._extensions..js (module.js:416:10)
    at Module.load (module.js:343:32)
    at Function.Module._load (module.js:300:12)
    at Function.Module.runMain (module.js:441:10)
    at startup (node.js:134:18)
    at node.js:962:3
rich-newman commented 8 years ago

You are getting the error because your syntax is a little wrong. You need a class definition if you have a using statement, see sample 208_convertImage.js for an example.

However there's a more fundamental problem, which is that you are trying to reference a WinRT dll. Edge builds a regular C# class library and then calls the code in it. As I'm sure you know, you can't directly reference a WinRT dll from a regular C# class library. There were some workarounds for this in Win8.1. However I think in Win10 the Edge C# compiler needs changing for Edge to be able to use the WinRT APIs (although I'm far from an expert in the UWP). Have a look at edge-cs.sln.

sgithens commented 8 years ago

Hi Rich,

Thanks for the quick response. I'll take a look at the syntax error/example to fix that.

For the WinRT dll, I didn't actually know that. Actually, I'm just in the progress of porting this code from a small C# app that was running on Windows 8 to my node app running on Windows 10. (ie. I'm not a real .NET developer, just doing enough to use some builtin NFC hardware and other things! :) )

Will take a look at that suggestion and report back.