Native C Code in Windows Phone Runtime - c

I am trying to use a existing c code in Windows Phone. For that I have created a Windows phone runtime component (C++) which has the legacy c code and referring it in my managed WP8 application.
I am facing issues in building the solution.
I have disable Precompiled headers option for "sample.c"
sample.h
int getNumber();
sample.c
#include <stdio.h>
#include"sample.h"
int main()
{
printf("This is a native C program.\n");
return 0;
}
int getNumber()
{
return 2014; // Sample value to return
}
NativeComponentRoot.cpp
#include "pch.h"
#include "NativeComponentRoot.h"
#include "sample.h"
using namespace NativeComponentRoot;
using namespace Platform;
WindowsPhoneRuntimeComponent::WindowsPhoneRuntimeComponent()
{
}
NumberGenerator::NumberGenerator()
{
}
int NumberGenerator::GetMeANumber()
{
int number = getNumber(); // Trying to invoke the function in sample.c
return number;
}
Issue
I couldnt find a proper walk through for integrating "C" (Not C++) lib/code in Windows Phone Runtime and using it in managed project.

Related

Application crashes using Py_DECREF on string PyObject in .dll built with Python C API

I am building a DLL to embed Python 2.7 (2.7.18 to be specific) in a larger application largely following python.org's C API documentation. When I believe I am applying Py_DECREF correctly to a PyString PyObject, any linking application crashes without error. The code is largely copied from a similar application built with python 3.8, 3.9 and 3.10 that in fact works.
// python_functions.h
__declspec(dllexport) int init_python();
// python_functions.c
#define PY_SSIZE_T_CLEAN
#include <Python.h>
#include <stdio.h> // printf
#include <string.h>
//#include <wchar.h> // wchar_t for python3 only
#include "python_functions.h"
__declspec(dllexport) int init_python() {
int rv = -1;
printf("attempting to initialize Python interpreter...\n");
// I have other python versions, Python27 not on path. set PYTHONHOME
Py_SetPythonHome("C:\\Python27\\"); // python27
//Py_SetPythonHome(L"C:\\Python38\\"); // python3X
Py_Initialize();
PyObject * pModule_Name = PyString_FromString("python_functions"); // python27 only
//PyObject * pModule_Name = PyUnicode_FromString("python_functions"); // python3X
if (!pModule_Name) {
printf("failed to convert module name to python string object\n");
return rv;
}
// can confirm pModule_Name != NULL && Py_REFCNT(pModule_Name) == 1 here
/*
load python module named "python_functions"
*/
// can also confirm pModule_Name != NULL && Py_REFCNT(pModule_Name) == 1 here
//Py_DECREF(pModule_Name); // If I uncomment this line, application linking .dll crashes
printf("pModule_Name successfully used\n");
rv = 0;
return rv;
}
// main.c
#include <stdio.h>
#include "python_functions.h"
int main(int argc, char **argv) {
int rv = 0;
// crashes without error if Py_DECREF(pModule_Name) in python_functions.c uncommented
printf("%d\n", init_python());
return rv;
}
Compiling the .dll with gcc 8.1.0 (MinGW-64) linking against C:\Python27\python27.dll:
gcc -IC:\Python27\include -LC:\Python27\libs -IC:\Python27 python_functions.c -shared -o python_functions.dll -lpython27
Compiling main with python27.dll and python_functions.dll in the same directory:
gcc main.c -o main.exe -L./ -lpython_functions
If I uncomment the line Py_DECREF(pModule_Name), which I thought I must do since PyString_FromString returns a new reference, then running main.exe crashes without return.
Should I be using something else or not doing reference counting on result from PyString_FromString or is this a larger problem with linking to python27?
If I make the commented/corresponding compiler changes to link against python 3.8+, no problems...
The only differences I know of are the significant changes to the Python C API between 2.7 and e.g. 3.8+ as well as the differences in python 2.X vs 3.X string representations. I believe PyString is appropriate here because when I actually implement and load a module name python_functions.py, it works...so long as I leave Py_DECREF(pModule_Name) commented out and the pointer leaked.
I also thought PyString_FromString might have a problem passing a string literal, but it accepts a const char *, documentation says it creates a copy, and even passing a copy of the string literal to another variable does not work.

How to color output in C for cross-platform app

I am new and I know how to color output only in Unix/Linux systems:
#include <stdio.h>
int main(void) {
printf("\033[1;31mRed Message\033[0m.");
}
But this is not works in Windows cmd.exe, only in Unix terminal.
I am writing cross-platform app and want to know how can I do this in Windows cmd.exe too.
This also does not works:
1.
#include <stdio.h>
int main(void) {
printf("%c[1;31mRed Message%c[0m", 27, 27);
}
2.
#include <stdio.h>
int main(void) {
printf("[1;31m Red Message [0m");
}
This works, but I think this is just a bug:
If I type system(""); before printf then it works.
#include <stdio.h>
int main(void) {
system("");
printf("\033[1;31m Red Message \033[0m");
}
Thanks
If you want to make your library crossplatform, I would use the following approach:
Have a library, with the same functions, let's say:
void printInRed(const char* string). (In a headerfile)
After that you write two or more implementations.
One for windows:
//TODO: Errorchecking
void printInRed(const char* string){
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
//TODO: Extract magic number
//See https://stackoverflow.com/a/4053879/13912132
SetConsoleTextAttribute(hConsole, 12);
puts(string);
}
And another one for unix-like OS:
//TODO: Errorchecking
void printInRed(const char* string){
printf("\033[1;31m%s\033[0m.", string);
}
Then you can check at compile time, which version to compile.
The first approach is to use #ifdefs, but this will make the code a bit messy.
Another approach would be to use a build-system like CMake to select at build time, which one to build. A buildsystem requires a bit of learning, but will help you to make maintaining a crossplatform library simpler.

error: "the procedure entry point.." when running exe after compiling with gcc / cygwin

I am learning c and I am using code::block
I have wrote this code "from ansi c book"
#include <stdio.h>
#include <stdlib.h>
float convertToCelsius(float f);
int main()
{
int start = 0;
int step = 5;
int upper = 300;
printf("%3c\t%6c\n",'F','C');
while(start < upper){
printf("%3d\t%6.3f\n", start, convertToCelsius(start));
start += step;
}
return 0;
}
float convertToCelsius(float f){
return (5.0/9)*(f-32);
}
when I run the code from the IDE "code::blocks" it compile and run without problems
but when I compile the c file using gcc in cygwin and try to run the exe file it gives me this message
the procedure entry point __cxa_atexit could not be located in the
dynamic link library C:\cygwin\home\username\convert.exe
I have search but couldn't find related direct answer
what is the problem ?

Allegro error in DevC++

I am running the latest DevC++ 5.5.3 and I need to use the Allegro 5.0.4 so I downloaded it from devpaks and install it common way. But when I want to run the project with allegro the compiler show me error "allegro.h: No such file or directory". I was looking for the answer but I haven't found the relevant one. And what more I have to use DevC++.
#include <stdio.h>
#include <allegro.h>
int main(void)
{
allegro_init();
allegro_message("Hello World");
return 0;
}
END_OF_MAIN()
Linker is set to -lalleg
Allegro 5 is not backward compatible with Allegro 4. It is a brand new library made by the same people.
Your code snippet is for Allegro 4.
The equivalent is:
#include <allegro5/allegro.h>
#include <allegro5/allegro_native_dialog.h>
int main(void)
{
al_init();
// al_init_native_dialog_addon(); // Introduced in 5.0.9
al_show_native_message_box( /* fill in params */ );
return 0;
}
You would need to link against the main Allegro library along with the native dialogs library.

Procedural parameters in xcode

is it possible to get procedural paramters working in xcode? I am working on an assignment for university, and would like to use the debugger in xcode to help me learn whats going on.
I know the code is at the very minimum compilable, because i was able to compile it with gcc, but xcode does not recognize the syntax.
the code i'm trying to run in xcode is:
#include <stdlib.h>
#include <stdio.h>
int main() {
int a = 50;
int b = 100;
void P(int *c, void R(int)) {
void Q(int p) {
printf("%d\n", p);
R(p+100);
}
if (a == b) R(b);
else {c = c + 25;
P(c, Q);
}
}
void Q(int p) {
printf("%d\n", p);
}
P(&a,Q);
}
Xcode uses the clang compiler by default, which does not support nested functions. To use gcc in Xcode, go to your project settings, select your target and change Compiler for C/C++/Objective-C to LLVM GCC 4.2. Now if you compile, gcc will complain, that nested functions are not enabled. Go back to your target settings, LLVM GCC 4.2 Language and under Other C Flags add -fnested-functions. Your project should compile now.

Resources