EDIT: I was finally able to make a "Hello, World!" project set. If you are also having Error #3500 problems, see my answer below for a working project set.
I'm currently making a "Hello, World!" Native Extension for Adobe AIR with FlashDevelop. My Native Extension is thus meant to be used by Windows-x86 platforms, which I'm programming on.
I've built the ANE via a (custom) batch file. The test AIR Application which uses that ANE compiles fine, but, like so many other people I've seen the posts of, I'm getting Error #3500: The extension context does not have a method with the name helloWorld.
I've been trying to understand what was going on for three days now, and, despite fixing several causes, I'm still getting the same error.
It seems the application runtime never actually gets to call the initializeExtension function, since DebugView doesn't trace anything, even though my initializer uses OutputDebugString(L"Extension initialized");.
I feel a bit bad for posting a lot of code, but after three days and dozens of webpages read, I'm just not sure where my problem is coming from.
Anyway, application building is done in 3 steps :
1) Building the DLL in Visual Studio with the Release flag. I'm posting that code as a response to Michael's comment below, however, I'm not sure the error is coming from there.
My native side is mainly made up of a header and a C++ file :
// -------------------------
// | NativeExtensionTest.h |
// -------------------------
#pragma once
#include "FlashRuntimeExtensions.h"
#ifdef __cplusplus
EXTERN_C
{
#endif
__declspec(dllexport) void initializeExtension(
void** dataToSet,
FREContextInitializer* contextInitializer,
FREContextFinalizer* contextFinalizer
);
__declspec(dllexport) void finalizeExtension(
void* extData
);
__declspec(dllexport) void initializeContext(
void* contextData,
const uint8_t* contextType,
FREContext context,
uint32_t* nFunctionsToSet,
const FRENamedFunction** functionsToSet
);
__declspec(dllexport) void finalizeContext(
FREContext context
);
__declspec(dllexport) FREObject helloWorld(
FREContext context,
void* functionData,
uint32_t argc,
FREObject argv[]
);
#ifdef __cplusplus
}
#endif
And here is the implementation of the functions:
// ------------------
// | HelloWorld.cpp |
// ------------------
#pragma once
#include "stdafx.h" // precompiled header ; includes cstdlib, cstring and windows.h
#include "FlashRuntimeExtensions.h"
#include "NativeExtensionTest.h"
using namespace std;
void initializeExtension(
void** dataToSet,
FREContextInitializer* contextInitializer,
FREContextFinalizer* contextFinalizer
)
{
dataToSet = NULL;
*contextInitializer = &initializeContext;
*contextFinalizer = &finalizeExtension;
}
void finalizeExtension(
void* extData
)
{ }
void initializeContext(
void* contextData,
const uint8_t* contextType,
FREContext context,
uint32_t* nFunctionsToSet,
const FRENamedFunction** functionsToSet
)
{
*nFunctionsToSet = 1;
FRENamedFunction* functions = (FRENamedFunction*)malloc(sizeof(FRENamedFunction)* (*nFunctionsToSet));
functions[0].name = (const uint8_t*)"helloWorld";
functions[0].function = &helloWorld;
functions[0].functionData = NULL;
*functionsToSet = functions;
}
void finalizeContext(
FREContext context
)
{ }
FREObject helloWorld(
FREContext context,
void* functionData,
uint32_t argc,
FREObject argv[]
)
{
char* hello = "Hello, World!";
unsigned int helloLength = strlen(hello) + 1;
FREObject ret;
FRENewObjectFromUTF8(helloLength, (const uint8_t*)hello, &ret);
return ret;
}
As alebianco requested, here is the build log when building the DLL. Note that I have an error at the very end of the log, at the end of the execution of a custom build batch file. I don't know where this error is coming from ; I didn't have that error last time I built that project. Besides, it's probably internal to FlashDevelop.
2) Building the ANE. I'm using a batch file to run the following commands:
To build the SWC, I invoke compc. After variable expansion, it looks like this:
compc -include-sources"C:\Users\Anthony Dentinger\Desktop\Native Extension Test\src" -output "C:\Users\Anthony Dentinger\Desktop\Native Extension Test\ANE Build Files\NativeExtHelloWorld.swc" -load-config "C:\Users\Anthony Dentinger\AppData\Local\FlashDevelop\Apps\flexsdk\4.6.0\frameworks\air-config.xml" -swf-version 14
My src folder contains a simple class:
package
{
import flash.events.EventDispatcher;
import flash.external.ExtensionContext;
public class NativeExtHelloWorld extends EventDispatcher {
private var extContext:ExtensionContext;
public function NativeExtHelloWorld()
{
extContext = ExtensionContext.createExtensionContext("NativeExtHelloWorld", "helloWorldContext");
}
public function helloWorld() : String
{
return String(extContext.call("helloWorld"));
}
}
}
In turn, after copying both the DLL from my Visual Studio folder and library.swf (which I extract from the SWC) to ANE Build Files\platforms\Windows-x86, I build the ANE with adt. After variable expansion, the command looks like this:
call adt -package -target ane "C:\Users\Anthony Dentinger\Desktop\Native Extension Test\ANE Build Files\HelloExtension.ane" "C:\Users\Anthony Dentinger\Desktop\Native Extension Test\ANE Build Files\descriptor.xml" -swc "C:\Users\Anthony Dentinger\Desktop\Native Extension Test\ANE Build Files\NativeExtHelloWorld.swc" -platform "Windows-x86" -C "C:\Users\Anthony Dentinger\Desktop\Native Extension Test\ANE Build Files\platforms\Windows-x86" .
Here is the extension descriptor that I provide adt:
<?xml version="1.0" encoding="utf-8"?>
<extension xmlns="http://ns.adobe.com/air/extension/3.1">
<id>NativeExtHelloWorld</id> <!--I'll later change that ID to something like com.example.myExt.HelloWorld-->
<name>Exension Name</name>
<description>Description of the Extension</description>
<versionNumber>0.0.1</versionNumber>
<copyright>© 2010, Examples, Inc. All rights reserved.</copyright>
<platforms>
<platform name="Windows-x86">
<applicationDeployment>
<nativeLibrary>HelloWorld.dll</nativeLibrary>
<initializer>initializeExtension</initializer>
<finalizer>finalizeExtension</finalizer>
</applicationDeployment>
</platform>
</platforms>
</extension>
3) Building the actual application. I'm using the default batch files made by FlashDevelop (with two modifications to include the ANE) for building AIR AS3 Projectors.
Note that, if I'm getting error #3500, it means (I suppose) that my application has successfully included my class from the ANE, since the constructor is working.
This is the application descriptor I'm using, in case that's helpful.
<?xml version="1.0" encoding="utf-8" ?>
<application xmlns="http://ns.adobe.com/air/application/15.0">
<id>TEST</id>
<versionNumber>1.0</versionNumber>
<filename>TEST</filename>
<name>TEST</name>
<initialWindow>
<title>TEST</title>
<content>TEST.swf</content>
<systemChrome>standard</systemChrome>
<transparent>false</transparent>
<visible>true</visible>
<minimizable>true</minimizable>
<maximizable>true</maximizable>
<resizable>true</resizable>
</initialWindow>
<supportedProfiles>extendedDesktop</supportedProfiles>
<extensions>
<extensionID>NativeExtHelloWorld</extensionID>
</extensions>
</application>
I'm using Flex (4.6.0) merged with the AIR SDK (22.0.0).
Have I done something wrong? What will help me fix this issue?
Once again, I'm sorry I posted quite a bit of code ; I've tried trimming down to what was most relevant.
Thanks in advance!
I've somehow solved the problem, and everything works now. I tried going back to see what I was doing wrong, but I can't quite figure it out. My guess is one of the batch files that copies and unzips the ANE was using the wrong target, so I ended up using the same old ANE, rather than using the ANE I was building (silly me).
Following Colonize.bat's request, here is a working Hello, World! example. I used VisualStudio 2013 and FlashDevelop to make this.
You can find several Error #3500 causes in the following files:
1_Build_DLL/Hello World/main.cpp
2_Build_ANE/lib/descriptor.xml
2_Build_ANE/src/HelloWorldExtension.as
Causes of some other errors I've read about (or encountered!) as also indicated in other files. In fact, the custom batch files indicate the procedure to follow in order to make and use your own ANE.
NOTE : Don't try to build directly from these files. I'm using batch files in order not to have to run commands and copy/paste/unzip files by hand, so the targets and environment variables will not be valid on your computer.
Related
Here, or here for a complete version, you can find a sample GRPC "Hello World" project for Unity. Only the first version, that is built for Unity and wrapped in a DLL is working perfectly fine in Unity IDE and on Standalone build. The Raw Grpc.Core files are referencing everything correctly in IDE but they have Marshaling problem.
Unfortunately, it cannot get build for UWP with IL2CPP backend. Unity builds the project and creates a .sln project. But Visual Studio always gives LNK2001 for GRPC properties on the final compilation.
Here are first error codes:
LNK2001 unresolved external _grpccsharp_init#0
LNK2001 unresolved external _grpccsharp_shutdonw#0
LNK2001 unresolved external _grpccsharp_version_string#0
...
Ok, thanks to #Sunius, I digged into it a little bit more. There are some points, I am going to add to the question:
There are two methods regarding referencing extern methods in GRPC C# package. They are named static and shared libs.
internal class DllImportsFromStaticLib
{
private const string ImportName = "__Internal";
[DllImport(ImportName)]
public static extern void grpcsharp_init();
[DllImport(ImportName)]
public static extern void grpcsharp_shutdown();
...
}
and
internal class DllImportsFromSharedLib
{
private const string ImportName = "grpc_csharp_ext";
[DllImport(ImportName)]
public static extern void grpcsharp_init();
[DllImport(ImportName)]
public static extern void grpcsharp_shutdown();
...
}
I tried to test it with the shared one, I got another linking error file which is a little bit different.
LNK2001 unresolved external _dlopen#8
LNK2001 unresolved external _dlsym#8
...
In two separate methods, extern methods are getting connected to the internal interface:
public NativeMethods(DllImportsFromStaticLib unusedInstance)
{
this.grpccsharp_init = DllImportsFromStaticLib.grpccsharp_init;
this.grpccsharp_shutdown = DllImportsFromStaticLib.grpccsharp_shutdonw;
...
}
and
public NativeMethods(DllImportsFromSharedLib unusedInstance)
{
this.grpccsharp_init = DllImportsFromSharedLib.grpccsharp_init;
this.grpccsharp_shutdown = DllImportsFromSharedLib.grpccsharp_shutdonw;
...
}
And which method will get called is defined here:
private static NativMethods LoadNativeMethodsUnity()
{
switch(PlatformApis.GetUnityRuntimePlatform())
{
case "IPhonePlayer":
return new NativeMethods(new NativeMethods.DllImportsFromStaticLib());
default:
return new NativeMethods(new NativeMethods.DllImportsFromSharedLib());
}
}
Some updates:
Thanks to #jsmouret, there is Stub.c file in his Grpc Github with fake methods, so Linker does not complain about Grpc_init methods anymore.
Next Error: dlopen, dlsym, dlerror:
First, I tried to use the same, Stub technique, but it did not help in this case, or maybe I did it wrong.
Thanks to #Sunius, I commented out all of "__Internal" dll import codes. So I am not getting any dlopen, dlsym, and dlerror errors.
Next Error: It happens from inside application, not the visual studio debugger. It tells me: "exception: to marshal a managed method, please add an attribute named 'MonoPInvokeCallback' to the method definition."
exception: error loading the embedded resource "Grpc.Core.roots.pem"
and
exception: To marshal a managed method, please add an attribute named 'MonoPInvokeCallback' to the method definition.
After I googled it, I know my options, but the question it, for which method should I do that?!
Thanks to my colleague Alice, #Sunius and #jsmouret, at the end, grpc works on UWP on Unity Platform through this steps:
Download Grpc.Core folder from Google Grpc Github.
Download Grpc Unity plugin from their official site.
Copy the runtime folder to your Grpc.Core folder. Please remove Grpc.Core.dll that you get from Grpc Unity Plugin, since we are using their source code.
Grpc should be in a folder called, Plugins in Unity, otherwise it will not be recognized.
Include this file in your runtime folder.
Include the Stub also from the Unity Plugin Inspector for WSA.
Find runtime .dll for Windows and include them in WSA from Unity Plugin Inspector.
By now, you should be getting _dlopen error.
Search through your Unity Solution with an IDE for "__Internal". There are not so many places, but comment them out. Also some methods that are depended on "__Internal"s, like dlopen and dlsym.
By now, you are not getting anymore build error but you need to make Grpc work.
Search for something like "DefaultSslRootsOverride" and comment out like below:
internal static class DefaultSslRootsOverride
{
const string RootsPemResourceName = "Grpc.Core.roots.pem";
static object staticLock = new object();
/// <summary>
/// Overrides C core's default roots with roots.pem loaded as embedded resource.
/// </summary>
public static void Override(NativeMethods native)
{
lock (staticLock)
{
//var stream = typeof(DefaultSslRootsOverride).GetTypeInfo().Assembly.GetManifestResourceStream(RootsPemResourceName);
//if (stream == null)
//{
// throw new IOException(string.Format("Error loading the embedded resource \"{0}\"", RootsPemResourceName));
//}
//using (var streamReader = new StreamReader(stream))
//{
// var pemRootCerts = streamReader.ReadToEnd();
// native.grpcsharp_override_default_ssl_roots(pemRootCerts);
//}
}
}
}
Search for something like "static void HandWrite" and add an attribute like something in below:
[MonoPInvokeCallback(typeof(GprLogDelegate))]
private static void HandleWrite(IntPtr fileStringPtr, int line, ulong threadId, IntPtr severityStringPtr, IntPtr msgPtr)
{
try
{
var logger = GrpcEnvironment.Logger;
string severityString = Marshal.PtrToStringAnsi(severityStringPtr);
string message = string.Format("{0} {1}:{2}: {3}",
threadId,
Marshal.PtrToStringAnsi(fileStringPtr),
line,
Marshal.PtrToStringAnsi(msgPtr));
switch (severityString)
{
case "D":
logger.Debug(message);
break;
case "I":
logger.Info(message);
break;
case "E":
logger.Error(message);
break;
default:
// severity not recognized, default to error.
logger.Error(message);
break;
}
}
catch (Exception e)
{
Console.WriteLine("Caught exception in native callback " + e);
}
}
I guess, you are done. In case, it did not work for your UWP, let me know, maybe I can help. :)
It looks like your plugin uses "__Internal" P/Invoke to call those native functions:
https://github.com/grpc/grpc/blob/befc7220cadb963755de86763a04ab6f9dc14200/src/csharp/Grpc.Core/Internal/NativeMethods.Generated.cs#L542
However, the linker cannot locate those functions and thus fails. You should change that code to either specify the DLL file name where the functions are implemented, or drop the source files with definitions for those functions into your Unity project. Or, if that code path isn't actually invoked (since you said it works on the standalone player), #ifdef it out from UWP build.
You can find more information about "__Internal" P/Invoke here:
https://docs.unity3d.com/Manual/windowsstore-plugins-il2cpp.html
Long Story short, OpenGL beginner/dabbler, Using GLFW for self study purpose.
I downloaded GLFW precompiled binaries from here
and I followed this tutorial (I know its for VS2010 specific but still)
I have read numerous questions on linker errors for VS2012 + GLFW 3.x set up.
None of them solved my problem.
Here is what I have in my code so far.
#define GLFW_DLL
#include <glfw3.h>
#pragma comment(lib,"glfw3.lib")
#pragma comment(lib,"glfw3dll.lib")
#pragma comment(lib,"opengl32.lib")
int main(int argc,char** argv)
{
GLFWwindow* window;
/* Initialize the library */
if (!glfwInit())
return -1;
/* Create a windowed mode window and its OpenGL context */
window = glfwCreateWindow(640, 480, "Hello World", NULL, NULL);
if (!window)
{
glfwTerminate();
return -1;
}
/* Make the window's context current */
glfwMakeContextCurrent(window);
/* Loop until the user closes the window */
while (!glfwWindowShouldClose(window))
{
/* Render here */
/* Swap front and back buffers */
glfwSwapBuffers(window);
/* Poll for and process events */
glfwPollEvents();
}
glfwTerminate();
return 0;
}
Here is what I have in my
VC include directory.
VC lib directory
VC linker input options
System32 DLL
Compilation is successful. However, I get
Why, what is wrong with this set up?
UPDATE::
I tried placing all the files (dlls, lib and headers) in the project folder, still no results,
I still get the same error message.
The easiest solution would be to use the static library rather than the dll, thus removing the need for the dll.
To do this you need to remove the define for GLFW_DLL so you file reads:
#include <glfw3.h>
main() // code follows
And you need to remove the glfw3dll.lib from the additional dependencies but leave glfw3.lib and opengl32.lib.
I'm new to native programming. I've been trying to fix the unsatisfiedLinkError past 8-9 hours but got no result. After a lot of googling and stackoverflowing, I got sick of fixing it, I'm posting my problem here. Somebody please please help me.
I'm using g++ compiler in windows 32bit environment.
Here are the files that I've created:
Demo.java
class Demo
{
// Declaration of the native method
public native int methodOfC(int arg1);
/*The native keyword tells the compiler that the implementation of this method is in a native language*/
/*Loading the library containing the implementation of the native method*/
static
{
System.out.println("Control is in Java.......going to call a C program......\n");
System.loadLibrary("try");
System.out.println("Congr8s no prob in CallApi.....\n");
}
public static void main(String[] args)
{
//invoking the native method
int sendToC,getFrmC;
if(args.length!=0) sendToC=Integer.parseInt(args[0]);
else sendToC=999;
Demo ob1=new Demo();
getFrmC=ob1.methodOfC(sendToC);
System.out.println("This is in Java......\n Got "+ getFrmC +" in return from C.");
}//end main
}//end Demo
Demo.h
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class Demo */
#ifndef _Included_Demo
#define _Included_Demo
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: Demo
* Method: methodOfC
* Signature: (I)I
*/
JNIEXPORT jint JNICALL Java_Demo_methodOfC
(JNIEnv *, jobject, jint);
#ifdef __cplusplus
}
#endif
#endif
DemoImp.c
#include <jni.h>
#include "Demo.h"
#include <stdio.h>
//definition of methodOfC()
JNIEXPORT int JNICALL Java_Demo_methodOfC(JNIEnv* exeenv, jobject javaobj, int getFrmJava)
{
printf("This is in the C program\n Got %d from java",getFrmJava);
printf("\n.......Exiting frm C\n");
return getFrmJava+1;
}
Here is how I compiled and run my prog.: screenshot here
C:\native>javac Demo.java
C:\native>javah -jni Demo
C:\native>g++ -c -l"C:\Java\jdk1.6.0_26\include" -l"C:\Java\jdk1.6.0_26\include\win32" DemoImp.c
C:\native>g++ -shared DemoImp.o -o try.dll
C:\native>java Demo 1234
Control is in Java.......going to call a C program......
Congr8s no prob in CallApi.....
Exception in thread "main" java.lang.UnsatisfiedLinkError: Demo.methodOfC(I)I
at Demo.methodOfC(Native Method)
at Demo.main(Demo.java:23)
C:\native>
I've already added "C:\native" in my system path variable.
I've uploaded all my files in mediafire. Here's the link native.zip
If possible please tell me how can I make 64bit version of dll. Thanks in advance.
You have missed out the package name in the DemoImp.c file.
The naming convention for C function is Java_{package_and_classname}_{function_name}(JNI arguments). The dot in package name shall be replaced by underscore.
EDIT: What is C++/CLI? I am programming in Visual studio, and as far as I know using C++... Also, The first error was solved by Peter's comment, but I am still stuck on the second.
I am brand new to the world of C++, and have previously done all my work in Java. I am unfamiliar with the use of pointers and garbage collection (though I believe I understand the concept) and I believe that may be the source of my problems. I am getting the following error messages:
1>Runner.cpp(6): error C3145: 'formOutOfTime' : global or static variable may not have managed type 'System::Windows::Forms::Form ^'
1> may not declare a global or static variable, or a member of a native type that refers to objects in the gc heap
1>Runner.cpp(22): error C2061: syntax error : identifier 'FormOutOfTime'
My code is like this:
PurpleHealth.cpp (This is the file I believe the system calls to start it all off):
#include "FormOutOfTime.h"
#include "FormParentalOverride.h"
#include "Runner.h"
using namespace PurpleHealth;
[STAThreadAttribute]
int main(array<System::String ^> ^args)
{
// Enabling Windows XP visual effects before any controls are created
Application::EnableVisualStyles();
Application::SetCompatibleTextRenderingDefault(false);
// Create the main window and run it
//Application::Run(gcnew FormOutOfTime());
Runner* runner = new Runner();
//delete runner;
return 0;
}
Runner.h (this is the header file I want to run all my main code, and launch the forms. I also struggle with the purpose behind the header files)
#include "stdafx.h"
#include "FormOutOfTime.h"
#include "FormParentalOverride.h"
class Runner
{
public:
Runner();
~Runner();
// functions
private:
void Go();
// member variables
};
And Finally Runner.cpp:
#include "stdafx.h"
#include "Runner.h"
#include "FormOutOfTime.h"
#include "FormParentalOverride.h"
//Variable Dclaration
System::Windows::Forms::Form^ formOutOfTime;//Error Here***************************
Runner::Runner()
{
// Do stuff if you need to
this->Go();
}
Runner::~Runner()
{
// Clear memory if you need to
}
void Runner::Go()
{
formOutOfTime = gcnew FormOutOfTime();//Error Here***************************
formOutOfTime->ShowDialog();
}
Please help me solve these messages, and even critique on form is appreciated. Thanks.
managed pointers cannot be declared at static or global scope. They can only be declared at function scope. Move the declaration of formOutOfTime from the top of the runner.cpp file to within the Go method
I think i'm getting crazy, im trying to compile a simple project to understand how to work with io_service and I cant compile it.
#include <iostream>
#include <boost/asio.hpp>
#include <boost/bind.hpp>
class testClass
{
unsigned int other_number;
unsigned int main_number;
boost::asio::io_service& io_serv;
public:
testClass(boost::asio::io_service& io) : other_number(0), io_serv(io), main_number(0){io_serv.post(boost::bind(&testClass::printNumbers, this));}
void changeNumber(int num)
{
io_serv.post(boost::bind(&testClass::doChangeNumber, this, num));
}
private:
void doChangeNumber(int num)
{
main_number = num;
}
void printNumbers()
{
std::cout<<"Main number is: "<<main_number<<" Other number is:"<<other_number<<std::endl;
other_number++;
Sleep(1000);
io_serv.post(boost::bind(&testClass::printNumbers, this));
}
};
void main()
{
boost::asio::io_service io_serv;
testClass tc(io_serv);
io_serv.run();
int num = 0;
while (true)
{
tc.changeNumber(num++);
Sleep(2000);
}
}
I did add in "project property->c/c++->general->additional include directories" the line: "C:\Program Files (x86)\boost_1_44_0";
And I did add in "project property->linker->additional library directories" the line: "C:\Program Files (x86)\boost_1_44_0\libs";
but nothing seems to work...
I'm using visual studio 2010..
there are no .lib files in boost_1_44_0\libs... I downloaded it 2 times from boost's site just to make sure..
no matter what I do, I always get LINK : fatal error LNK1104: cannot open file 'libboost_system-vc100-mt-gd-1_44.lib'
You can build the Boost libs on your local system using bjam as described here (Section 5.2). Once you have done that you should be good to go - use this from a Visual Studio command prompt and make sure your project has the correct LIB path.
The prebuilt libs will only be there by default if you use the installer from Boost Pro Computing, I believe.