extern

How to define extern variable along with declaration?

橙三吉。 提交于 2019-12-03 23:57:05
Wiki says: The extern keyword means "declare without defining". In other words, it is a way to explicitly declare a variable, or to force a declaration without a definition. It is also possible to explicitly define a variable, i.e. to force a definition. It is done by assigning an initialization value to a variable . That means, an extern declaration that initializes the variable serves as a definition for that variable . So, /* Just for testing purpose only */ #include <stdio.h> extern int y = 0; int main(){ printf("%d\n", y); return 0; } should be valid ( compiled in C++11 ). But when

从Win服务启动UI程序

我的梦境 提交于 2019-12-03 23:46:05
从Win服务启动UI程序 从windows服务启动一个带UI程序的界面,这个需求在xp中是很随意的,从Vista开始似乎没有那么随意了,因为Vista中加入了Session的概念,那么什么是Session,我想这篇文章介绍的应该比我权威的多。 Session隔离介绍 明白了Session的概念后,我将通过Win32 API来实现从windows服务启动一个带UI的界面 (从Session 0中启动Session *的程序) ,这个实现过程是我从C++代码翻译过来的。 实现的思路 找到一个除Session 0之外的活动Session 通过Session ID获取用户Token 通过Token来启动UI程序 涉及的Win32 API WTSGetActiveConsoleSessionId 获取活动的Session ID WTSQueryUserToken 根据Session ID获取用户Token CreateProcessAsUser 使用用户Token来启动UI程序 实现代码 public class ProcessAsUser { public struct SECURITY_ATTRIBUTES { public uint nLength; public uint lpSecurityDescriptor; public bool bInheritHandle; }

C# 嵌入第三方EXE界面到panel中

≡放荡痞女 提交于 2019-12-03 23:22:00
C#可以通过windows API,将第三方程序嵌入到panel中,并且可以隐藏程序边框。 问题: 焦点在内部程序时,主窗口失去焦点; 与内部EXE如何通讯? 代码如下: public partial class FrmIn : Form { public FrmIn() { InitializeComponent(); } [DllImport("User32.dll", EntryPoint = "SetParent")] private static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent); [DllImport("user32.dll", EntryPoint = "ShowWindow")] private static extern int ShowWindow(IntPtr hwnd, int nCmdShow); [DllImport("user32.dll", SetLastError = true)] private static extern bool MoveWindow(IntPtr hwnd, int x, int y, int cx, int cy, bool repaint); [DllImport("user32.dll")] public static

extern enum in c++

和自甴很熟 提交于 2019-12-03 13:05:41
I have an enum I have declared in some .h file: typedef enum { NONE, ONE, TWO, THREE } MYENUM; in a seperate .cpp I cannot do this: extern enum MYENUM; //works extern MYENUM TWO; //makes sence, TWO is not an INSTANCE of MYENUM... how would one do so without including the whole header where the enum is declared? You can't use an incomplete type. You can only pass around pointers to it. This is because until the type is completed, the compiler doesn't know how big it is. OTOH a pointer is the size of a data pointer, no matter what type it's pointing to. One of the things you can't do with an

C++ dll的创建和使用

谁说胖子不能爱 提交于 2019-12-03 12:14:28
在介绍Dll之前先了解下常见三种函数调用约定。 参考: https://www.cnblogs.com/yejianyong/p/7506465.html 我们使用的VS默认使用的函数调用约定是__cdel,而Windows API默认的调用约定是__stdcall。我们在使用一个dll的接口时,一定要确保你使用接口时的调用约定和接口定义时的调用约定一致。因为不同的调用约定,函数的栈内存释放的方式不同。 然后我们再了解下extern C的作用,参考 https://www.cnblogs.com/carsonzhu/p/5272271.html 。 如果我们使用C++进行程序开发,开发过程中使用到了C语言写的库函数,则我们就需要使用extern C 来修饰,来告诉编译器,这部分代码使用C语言语法进行编译链接。 例如: #ifdef __cplusplus extern "C" { #endif #include "legacy_C_header.h" #ifdef __cplusplus } #endif 这个就是它允许你在你的C ++代码中使用这个C头文件,因为宏“__cplusplus”将被定义。 下面我们开始学习C++Dll的创建和使用 C++创建的dll既可以给C++工程使用也可以给C#工程使用。 C++导出函数,在DLL中有一张导出表,记录着需要导出的函数

C语言存储类别和链接

▼魔方 西西 提交于 2019-12-03 12:11:48
目录 C语言存储类别和链接 存储类别 存储期 五种存储类别 C语言存储类别和链接 ​ 最近详细的复习C语言,看到存储类别的时候总感觉一些概念模糊不清,现在认真的梳理一下。C语言的优势之一能够让程序员恰到好处的控制程序,可以通过C语言的内存管理系统指定变量的作用域和生存周期,实现对程序的控制。 存储类别 基本概念 对象 :在C语言中所有的数据都会被存储到内存中,被存储的值会占用一定的物理内存,这样的一块内存被称为 对象 ,它可以储存一个或者多个值,在储存适当的值时一定具有相应的大小。(C语言对象不同于面向对象语言的对象) 标识符 :程序需要一种方法来访问对象,这就需要声明变量来实现,例如: int identifier = 1 ,在这里 identifier 就是一个标识符,标识符是一个名称并遵循变量的命名规则。所以在本例中 identifier 即是C程序指定硬件内存中的对象的方式并提供了存储的值的大小“1”。在其它的情况中 int * pt 、 int arr[10] ,pt就是一个标志符,它指定了储存地址的变量,但是表达式*p不是一个标志符,因为它不是一个名称。 arr 的声明创建了一个可容纳10个 int 类型元素的对象,该数组的每一个元素也是一个对象。 作用域 :描述程序中可访问标识符的区域。因为一个C变量的作用域可以是块作用域、函数作用域、文件作用域和函数原型作用域。

Can a variable be redeclared as auto that deduced to the same type? [duplicate]

陌路散爱 提交于 2019-12-03 10:18:05
This question already has answers here : Does a declaration using “auto” match an extern declaration that uses a concrete type specifier? (3 answers) Is the following allowed by the standard? #include <iostream> extern int a; auto a = 3; int main(int, char**) { std::cout << a << std::endl; return 0; } clang accepts the code. g++ complains for conflicting declaration. Its not much clear to me from the standard, but then, there is this written section 7.1.6.4 auto specifier A program that uses auto in a context not explicitly allowed in this section is ill-formed. Better read the mentioned

Taking a screenshot C++ cli

匿名 (未验证) 提交于 2019-12-03 10:09:14
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 由 翻译 强力驱动 问题: I've seen the following code that takes a screenshot and saves it as jpg, I've managed to compile and run it as win32 CONSOLE application, But when I tried to use the following code in A windowsForm/CLI (there is just a button that should take a screenshot) project, I got the following errors: 1 > screenshoter . obj : warning LNK4248 : unresolved typeref token ( 0100002C ) for 'Gdiplus.GpCachedBitmap' ; image may not run 1 > screenshoter . obj : error LNK2028 : unresolved token ( 0A000017 ) "extern " C " int __stdcall ReleaseDC

第9课.函数重载分析(下)

戏子无情 提交于 2019-12-03 09:53:42
1.重载与指针 typedef int (*PFUNC)(int a); int c = 0; PFUNC p = func; c = p(1); //which function to call 第一个 函数重载遇上函数指针 将重载函数名赋值给函数指针时 a.根据重载规则挑选与函数指针参数列表一直的候选者 b. 严格匹配 候选者和 函数类型 与函数指针的函数类型。(不单单是函数名和参数列表来决定了) 注意 a.函数重载 必然发生在同一个作用域中 (c语言只有一个作用域:全局作用域;c++中不只一个作用域,还有类作用域,在其中都可以定于函数。你不可能这个作用域去调用另外一个作用域中的函数) 2.c++和c相互调用 === c++直接编译c语言的源码是没有问题的,但是在工程中有些c代码已经不是源码了,已经被编译成了目标文件。即我们在工程中用到第三方的库,这些库是用c语言进行编译的。让c++也能够去编写c语言编译的库。 a.目的是编译器能够兼容c语言的编译方式 b.extern关键字能强制让c++编译器进行c方式的编译 extern "C" { //do C-style compilation here } 问题:如何保证一段c代码只会以c的方式被编译? __cplusplus是c++编译器内置的标准红定义。意义:确保c代码以统一的c方式被编译成目标文件。 #ifdef _

Behaviour of extern in C

匿名 (未验证) 提交于 2019-12-03 09:06:55
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: If I declare int Array[10]; in file1.c In file2.c if I I have a function like this extern int *Array; fun() { Array[0]=10; } Is there any problem with this or not? 回答1: Yes there is a problem. You are declaring a Array to be a pointer , while instead it's an array . The two are very distinct objects and here you're basically providing false information to the compiler. Note that the C syntax to access an element is Array[0] in both cases, but if Array is a pointer variable the machine code needed will be different from if Array is instead an