Indispensable Windows Native (1), windowsnative

Source: Internet
Author: User

Indispensable Windows Native (1), windowsnative

[Download source code]


Indispensable Windows Native (1)-C language: hello c



Author: webabcd


Introduction
Indispensable C language for Windows Native

  • Call C/C ++ in Windows Store Apps
  • Hello c



Example
1. demonstrate how to call C/C ++ in Windows Store Apps (C #) and create a Windows Runtime Component (C ++) project
NativeDll/Simple. h

/**. H header file * // ensure that the header file is compiled only once (even if it is referenced multiple times, it is compiled only once) # pragma once namespace NativeDll // namespace {public ref class Simple sealed // class {public: int Add (int x, int y); // method };}

NativeDll/Simple. cpp

/**. Cpp implementation file ** to support Windows Runtime Component, Microsoft created the Visual C ++ component extensions (C ++/CX) is introduced ), it can be seen as a bridge between the "Caller" and "C/C ++". The metadata is windows metadata (. winmd) files * to enable the "Caller" to call Windows Runtime Component, C ++/CX has its own data types, for example, the String is of the Platform: String ^ type, in this way, the caller can call * for knowledge about C ++/CX, see: https://msdn.microsoft.com/en-us/library/hh755822.aspx */# include "pch. h "// pre-compiled header file # include" Simple. h "// the header file to be implemented // The namespace using namespace NativeDll defined in the header file; // The method int Simple: Add (int x, int y) in the header file) {return x + y ;}

NativeDemo/Simple. xaml. cs

/** Demonstrate how to use C # To Call The Windows Runtime Component (C ++) corresponding to C ++ **. For details, refer to the NativeDll project * Note: The Windows Runtime Component project will be generated. winmd file, winmd-Windows Metadata, Which is language-independent */using Windows. UI. xaml. controls; using Windows. UI. xaml. navigation; namespace NativeDemo. demo {public sealed partial class Simple: Page {public Simple () {this. initializeComponent ();} protected override void OnNavigatedTo (NavigationEventArgs e) {// call the NativeDll function in C ++. simple simple = new NativeDll. simple (); int result = simple. add (1, 1); lblMsg. text = "1 + 1 =" + result. toString ();}}}

Readme.txt

1. To use the C language, you need to select the corresponding. c file-> right-click-> properties-> c/c ++-> advanced-> compile as C ++ code (/TP) 2. For example, if you want to use strcpy, you will be warned in vs, asking you to use strcpy_s, but strcpy_s is owned by Microsoft. To remove this warning, you can do this:) define # define _ CRT_SECURE_NO_WARNINGS B at the beginning of the file) or use the following method once and for all: dll project-> right-click-> properties-> c/c ++-> pre-processor-> Add "_ CRT_SECURE_NO_WARNINGS" in "pre-processor definition" 3. debug the local code: select solution-> right-click-> properties-> debug-> debugger type-> select "hybrid (managed and local)" 4. How to Create c code, the default format is UTF-8: Modify the "hfile" template file in a directory similar to D: \ Program Files (x86) \ Microsoft Visual Studio 12.0 \ VC \ vcprojectitems. h and newc ++ file. cpp File Format: UTF-8


2. hello c
CHello. h

// C. h file // header file is generally used for placement: macro definitions to be exposed, global variable declaration, function declaration // prevent secondary compilation of the same file. For example, you have two c files, both of which include the same header file. during compilation, these two c files will be compiled together, the Declaration conflict problem occurs. # ifndef _ MYHEAD_HELLO _ // is the abbreviation of if not defined, if _ MYHEAD_HELLO _ is not defined, execute this section # define _ MYHEAD_HELLO _ // write the c language code in c ++ # ifdef _ cplusplus // If currently, the c ++ environment extern "C" // tells c ++ that the {} block is written in the c language code {# endif // function declaration char * demo_cHello (char * name ); # ifdef _ cplusplus // if the current environment is c ++} # endif/* // in windows, this can be abbreviated as # ifdef _ cplusplusextern "C" # endifchar * demo_cHello (char * name); */# endif/# ifndef _ MYHEAD_HELLO _

CHello. c

/** Hello c */# include "pch. h "// pre-compiled header file # include" cHello. h "// introduce the header file to be implemented # include" cHelper. h "// the header file char * demo_cHello (char * name) {return str_concat2 (" hello: ", name);} // This demo cannot demonstrate the main function, so here are some text descriptions about the main function. // The main function is an entry function and cannot be called by other functions. // assume that the command is: executable File name parameter 1 parameter 2 parameter 3int main (int argc, char * argv []) // The main function can also be return value without any parameters, that is: int main (void) {} or void main (void) {} can both be {// argc is the number of parameters; argv is the parameter value // argc-equals to 4 (note: "executable file name" is also a parameter) // argv [0]-executable file name; argv [1]-parameter 1; argv [2]-parameter 2; argv [3]-parameter 3 // return 0 indicates normal return 0 ;}



OK
[Download source code]

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.