Close and Reopen PDF's in C - c

I am trying to write a function that when called, notes what pdf's are open in Acrobat, closes them, runs something else that will modify those files, and then reopens them.
Currently I am getting a bunch of HWND's and storing them in global variables accessed in close_pdfs(). This is shown below (I know I've been lazy and have magic numbers, just trying to get it working):
char windows[16][64]; // up to 16 windows with name length 64
int n_windows;
HWND tobedestroyed[16];
...
...
void close_pdfs() {
n_windows = 0;
EnumWindows(EnumWindowsProc, NULL);
for (int i = 0; i< n_windows; i++) {
printf("%s\n", windows[i]);
DestroyWindow(tobedestroyed[i]);
}
}
void reopen_pdfs() {
//this should iterate and reopen the closed pdf's
}
BOOL CALLBACK EnumWindowsProc(HWND hwnd, LPARAM lParam) {
char title[80];
GetWindowText(hwnd,title,sizeof(title));
if (!strcmp(&title[strlen(title)-4], ".pdf")) {
strcpy(windows[n_windows], title);
tobedestroyed[n_windows] = hwnd;
n_windows ++;
}
return TRUE;
}
These are called by:
close_pdfs();
system("modify_pdfs.exe");
reopen_pdfs();
Currently I can't get the pdf's to close, however I also think it is likely that there are better ways to tackle this problem.
Any help on how to complete this task as described in the first paragraph would be greatly appreciated.

Related

How can I catch the timeout exception in a third dll function,I use c language in Windows

How can I catch the timeout exception in a third dll function,I use c language in Windows
I want to catch a timeout Exception while call a thirdly dll function, you know the function takes a long while, and I need it return a value in limited time, if it doesn't return in the time, I will give it a default value.
I have to look for so much infomation about but it doesn't work.
I get the two point:
1.use the alarm function in ,but it only work in Linux,I can't use it in Windows even I use the MinGW standerd GCC complier.
2.use the timeSetEvent function in and the setjmp/longjmp function in ,the three function maybe so closed to take it work.but I use them caused the programe dump,windows pops a DialogMessage say something wrong.
I give the code and the picture like this :
`
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <setjmp.h>
jmp_buf j;
/**
* 时间中断函数
*/
void PASCAL OneMilliSecondProc(UINT wTimerID, UINT msg, DWORD dwUser, DWORD dwl, DWORD dw2) {
printf("Timout!\n");
longjmp(j,1);
}
int longTimeFunction(){
while (1) {
printf("operating...\n");
Sleep(1000);
}
return 0;
}
int main(){
HANDLE hHandle;
UINT wTimerRes_1ms;//定义时间间隔
UINT wAccuracy; //定义分辨率
UINT TimerID_1ms; //定义定时器句柄
wTimerRes_1ms = 5000;
if((TimerID_1ms = timeSetEvent(
wTimerRes_1ms,
wAccuracy,
(LPTIMECALLBACK)OneMilliSecondProc, // 回调函数
(DWORD)(1), // 用户传送到回调函数的数据;
TIME_PERIODIC//周期调用定时处理函数
)) == 0) {
printf("start!!!!!!!!!!!\n");
} else {
printf("end!!!!!!!!!!!\n");
}
int temp = 0;
if(setjmp(j) == 0){
temp = longTimeFunction();
}else{
printf("xxxxxx...\n");
temp = -1;
}
printf("%d\n", temp);
return 0;
}
`
Unlike UNIX signals, timeSetEvent doesn't interrupt a thread, the callback runs in parallel and longjmping across threads is undefined behavior.
Concerning your actual question, this is a bad idea. Such an abortion could leave the library in an inconsistent state.
Instead, try to get the library vendor to offer an API that accepts a timeout, or use another library that already supports it.

Pebble Menu Layer Crash

I've been designing a personal news feed app for the original pebble which displays news content in a menu layer. I have two C files, one which contains a window that displays a loading image and handles the data transfer between the js on the phone and my watch app. The data is saved into some extern variables which are shared between the files but is irrelevant in the example I show below (all of that works fine I believe).
I'm calling news_list_window_init() in my main .c file within a function when certain conditions are met. Right now I'm just trying to display a window with a scrollable menu to test that my menu looks acceptable. The problem I'm having is that my app crashes when I try to scroll. The window appears, all my menu items are there but when I scroll the app exists with:
ault_handling.c:78> App fault! {f5ec8c0d-9f21-471a-9a5c-c83320f7477d} PC: 0x800fd5b LR: ???
Program Counter (PC) : 0x800fd5b ???
Link Register (LR) : ??? ???
I've tested my .c file below independently, where I create a new project with only this code, comment out the non-relevant news_list.h and externs.h header files, and comment in the main function at the bottom of the file. This works fine, my menu scrolls and there are no crashes and everything looks fine.
I don't see how the problem could be in my main file, because the only function I call in it which is contained in this file is news_list_window_init(), moreover the menu does indeed display correctly. I can even close the app properly with the back button. Trying to scroll however crashes the application. I'm sort of at a loss as to what could be causing this error. Does anybody have any suggestions? Thanks!
This is the relevant .c file:
// news_list.c
#include "pebble.h"
#include "news_list.h"
#include "externs.h"
#define NUM_MENU_SECTIONS 1
static Window *news_list_window;
static MenuLayer *menu_layer;
static uint16_t menu_get_num_sections_callback(MenuLayer *menu_layer, void *data) {
return NUM_MENU_SECTIONS;
}
static uint16_t menu_get_num_rows_callback(MenuLayer *menu_layer, uint16_t section_index, void *data) {
return str_count; // Variable story count
}
static int16_t menu_get_header_height_callback(MenuLayer *menu_layer, uint16_t section_index, void *data) {
return MENU_CELL_BASIC_HEADER_HEIGHT;
}
static void menu_draw_header_callback(GContext* ctx, const Layer *cell_layer, uint16_t section_index, void *data) {
menu_cell_basic_header_draw(ctx, cell_layer, "Header");
}
static void menu_draw_row_callback(GContext* ctx, const Layer *cell_layer, MenuIndex *cell_index, void *data) {
menu_cell_basic_draw(ctx, cell_layer, "Menu Item", NULL, NULL);
}
static void menu_select_callback(MenuLayer *menu_layer, MenuIndex *cell_index, void *data) {
// Currently Empty
}
int16_t menu_get_cell_height_callback(struct MenuLayer *menu_layer, MenuIndex *cell_index, void *callback_context)
{
return 40;
}
static void news_list_window_load(Window *window) {
// Now we prepare to initialize the menu layer
Layer *window_layer = window_get_root_layer(window);
GRect bounds = layer_get_frame(window_layer);
// Create the menu layer
menu_layer = menu_layer_create(bounds);
menu_layer_set_callbacks(menu_layer, NULL, (MenuLayerCallbacks){
.get_num_sections = menu_get_num_sections_callback,
.get_num_rows = menu_get_num_rows_callback,
.get_header_height = menu_get_header_height_callback,
.draw_header = menu_draw_header_callback,
.draw_row = menu_draw_row_callback,
.select_click = menu_select_callback,
.get_cell_height = menu_get_cell_height_callback,
});
// Bind the menu layer's click config provider to the window for interactivity
menu_layer_set_click_config_onto_window(menu_layer, window);
layer_add_child(window_layer, menu_layer_get_layer(menu_layer));
}
static void news_list_window_unload(Window *window) {
// Destroy the menu layer
menu_layer_destroy(menu_layer);
}
void news_list_window_init() {
news_list_window = window_create();
window_set_window_handlers(news_list_window, (WindowHandlers) {
.load = news_list_window_load,
.unload = news_list_window_unload,
});
window_stack_push(news_list_window, true);
}
void news_list_window_deinit() {
window_destroy(news_list_window);
}
// int main(void) {
// news_list_window_init();
// app_event_loop();
// news_list_window_deinit();
// }
This is the relevant .h file:
// news_list.h
#ifndef NEWS_LIST_H
#define NEWS_LIST_H
// Public Function list
void news_list_window_init(void);
void news_list_window_deinit(void);
#endif
I've had this happen when I'm running really low on memory, or if I'm corrupting memory somehow.
I'd suggest checking all return values for NULL and logging and bailing if they are (menu_layer_create, malloc etc.).
Also, try logging how much free memory you have - if you are working on an original Pebble, and allocating large memory buffers for communication, you can very quickly run out of memory.
Finally, walk through all code that allocates and uses memory to be absolutely sure you are not accidentally writing past the end of an array, or passing a value allocated on the stack into a pebble call. I like to use calloc rather than malloc to be sure I don't think I have a value in a malloc'd structure when I don't.
I've been here before - it isn't easy, but likely totally unrelated to your UI code - the crash is just a symptom. If all else fails maybe post the complete code in github so that we can take a look at the complete app...
Damian

Is there a Linux equivalent of SetWindowPos?

A while ago I wrote a script in C that used the Windows API functions EnumWindows, SetWindowPos and SetForegroundWindow to automatically arrange windows (by title) in a particular layout that I commonly wanted.
Are there Linux equivalents for these functions? I will be using Kubuntu, so KDE-specific and/or Ubuntu-specific solutions are fine.
The best way to do this is either in the window manager itself (if yours supports extensions) or with the protocols and hints designed to support "pagers" (pager = any non-window-manager process that does window organization or navigation things).
The EWMH spec includes a _NET_MOVERESIZE_WINDOW designed for use by pagers. http://standards.freedesktop.org/wm-spec/wm-spec-1.3.html#id2731465
Raw Xlib or Xcb is pretty rough but there's a library called libwnck specifically designed to do the kind of thing you're talking about. (I wrote the original library long ago but it's been maintained by others forever.) Even if you don't use it, read the code to see how to do stuff. KDE may have an equivalent with KDE-style APIs I'm not sure.
There should be no need to use anything KDE or GNOME or distribution specific since the needed stuff is all spelled out in EWMH. That said, for certain window managers doing this as an extension may be easier than writing a separate app.
Using old school X calls directly can certainly be made to work but there are lots of details to handle there that require significant expertise if you want to iron out all the bugs and corner cases, in my opinion, so using a WM extension API or pager library would be my advice.
#andrewdotn has a fine answer there but you can do this old school as well fairly simply by walking the tree starting at the root window of the display using XQueryTree and fetching the window name with XFetchName then moving it with XMoveWindow. Here is an example that will list all the windows and if any are called 'xeyes' they get moved to the top left. Like most X programs, there is more to it and this should probably be calling XGetWindowProperty to fetch the _NET_WM_NAME extended window manager property but the example works ok as a starter. Compile with gcc -Wall -g -o demo demo.c -lX11
#include <X11/Xlib.h>
#include <stdio.h>
#include <string.h>
static int
EnumWindows(Display *display, Window window, int depth)
{
Window parent, *children;
unsigned int count = 0;
int r = 1, n = 0;
char *name = NULL;
XFetchName(display, window, &name);
for (n = 0; n < depth; ++n) putchar(' ');
printf("%08x %s\n", (int)window, name?name:"(null)");
if (name && strcmp("xeyes", name) == 0) {
XMoveWindow(display, window, 5, 5);
}
if (name) XFree(name);
if (XQueryTree(display, window, &window, &parent, &children, &count) == 0) {
fprintf(stderr, "error: XQueryTree error\n");
return 0;
}
for (n = 0; r && n < count; ++n) {
r = EnumWindows(display, children[n], depth+1);
}
XFree(children);
return r;
}
int
main(int argc, char *const argv[])
{
Display *display = NULL;
if ((display = XOpenDisplay(NULL)) == NULL) {
fprintf(stderr, "error: cannot connect to X server\n");
return 1;
}
EnumWindows(display, DefaultRootWindow(display), 0);
XCloseDisplay(display);
return 0;
}
Yes, you can do this using the X Windows protocol. It’s a very low-level protocol so it will take some work. You can use xcb_query_tree to find the window to operate on, and then move it with xcb_configure_window. This page gives some details on how to do it. There’s a basic tutorial on using the library those functions come from, but you’ll probably want to Google for a better one.
It may seem daunting, but it’s not too bad. Here’s a 50-line C program that will move all your xterms 10px to the right:
#include <stdio.h>
#include <string.h>
#include <xcb/xcb.h>
void handle(xcb_connection_t* connection, xcb_window_t window) {
xcb_query_tree_reply_t *tree = xcb_query_tree_reply(connection,
xcb_query_tree(connection, window), NULL);
xcb_window_t *children = xcb_query_tree_children(tree);
for (int i = 0; i < xcb_query_tree_children_length(tree); i++) {
xcb_get_property_reply_t *class_reply = xcb_get_property_reply(
connection,
xcb_get_property(connection, 0, children[i], XCB_ATOM_WM_CLASS,
XCB_ATOM_STRING, 0, 512), NULL);
char* class = (char*)xcb_get_property_value(class_reply);
class[xcb_get_property_value_length(class_reply)] = '\0';
if (!strcmp(class, "xterm")) {
/* Get geometry relative to parent window */
xcb_get_geometry_reply_t* geom = xcb_get_geometry_reply(
connection,
xcb_get_geometry(connection, window),
NULL);
/* Move 10 pixels right */
uint32_t values[] = {geom->x + 10};
xcb_configure_window(connection, children[i],
XCB_CONFIG_WINDOW_X, values);
}
/* Recurse down window tree */
handle(connection, children[i]);
}
}
int main() {
xcb_connection_t *connection;
const xcb_setup_t *setup;
connection = xcb_connect(NULL, NULL);
setup = xcb_get_setup(connection);
xcb_screen_iterator_t screen = xcb_setup_roots_iterator(setup);
handle(connection, screen.data->root);
return 0;
}
There’s no error-checking or memory management, and what it can do is pretty limited. But it should be straightforward to update into a program that does what you want, or to turn it into a general-purpose helper program by adding command-line options to specify which windows to operate on and which operations to perform on them.
As it seems you are not looking specifically for a solution in code, but rather in a desktop environment, you need to take a look at one of the window managers that handle the window placement in such a desktop environment.
KDE's KWin's Window Attributes
Compiz (GNOME) has "Window Rules" and "Place Windows" in the CompizConfig Settings Manager application. See e.g. here
Openbox seems a lot harder to get right, although they link to a GUI tool at the bottom of this page.
The problem with using X directly is that X in itself knows nothing about your desktop environment (panels, shortcuts, etc.) and you'll have to compensate manually.
After googling for this, I'm surprised KDE is the only one that has a simple way to do this.

Trouble calling a .DLL function from a Console Application

I really need your help because I'm having many problems integrating one .DLL function into my Console Application.This .DLL consists on getting 2 chars(Char a and Char b)(Input) and adding them to one char.For example:
Char A=H
Char B=I
Output=HI
But here's the problem.When I compile the console application and when I run it,it says that the function hasn't been detected.And here's my question..Why the hell doesn't it find the function even though in the .def file I've listed the LIBRARY and the only exported function?Please help me.
THIS IS THE .DLL SOURCE
#include "stdafx.h"
char concatenazione(char a,char b)
{
return a+b;
}
**THIS IS THE .DEF FILE OF THE .DLL**
LIBRARY Concatenazione
EXPORTS
concatenazione #1
**And this is the dllmain.cpp**
#include "stdafx.h"
BOOL APIENTRY DllMain( HMODULE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved )
{
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
case DLL_THREAD_ATTACH:
case DLL_THREAD_DETACH:
case DLL_PROCESS_DETACH:
break;
}
return TRUE;
}
**This is the Console Application partial source(For now it just includes the functions import part)**
#include <iostream>
#include <windows.h>
#include <stdio.h>
typedef int (__cdecl *MYPROC)(LPWSTR);
int main( void )
{
HINSTANCE hinstLib;
MYPROC ProcAdd;
BOOL fFreeResult, fRunTimeLinkSuccess = FALSE;
// Get a handle to the DLL module.
hinstLib = LoadLibrary(TEXT("C:\\ConcatenazioneDiAyoub.dll"));
// If the handle is valid, try to get the function address.
if (hinstLib != NULL)
{
ProcAdd = (MYPROC) GetProcAddress(hinstLib, "concatenazione");
// If the function address is valid, call the function.
if (NULL != ProcAdd)
{
fRunTimeLinkSuccess = TRUE;
(ProcAdd) (L"Message sent to the DLL function\n");
}
// Free the DLL module.
fFreeResult = FreeLibrary(hinstLib);
}
// If unable to call the DLL function, use an alternative.
if (! fRunTimeLinkSuccess)
printf("Message printed from executable\n");
return 0;
}
**The Console Applications runs fine but the output is "Message printed from executable".This means that the function hasn't been detected.**
Ok, I looked at what you have above and wondered where in your code you import the dll. I'm kind of new at this myself, so I found this article on how to do it.
If I understand correctly, in your console application you need to have a line like (assuming the name of your dll is "your.dll":
[DllImport("your.Dll")]
by the way, welcome to Stack Overflow, I noticed it is your first day! CHEERS!

Visual Studio open cmd.exe with Consolas

I have a program written in C and it's beeing developed under Visual Studio 2012.
My program uses characters not available in Raster fonts, so I changed cmd.exe font to Consolas and my characters (á, é, í, etc.) worked.
But, when I open my program, the console still remains in Raster font. I don't know where I should change to make the Consolas globally accepted.
Thanks in advance.
Here some code I scraped together from several sources.
Allows me to use codepages, avoids unicode.
However, wprintf() etc should work, too.
Only consolas and lucida console work sofar. Includes an attempt to get a similar size.
Checks include the validitycheck of the nFont index.
That may be unneeded, in which case you can leave out the undocumented call to kernel32 routine GetNumberOfConsoleFonts().
Hans says it won't work on XP, docs say minimum _WIN32_WINNT is 0x500 which would include XP. Haven't tried yet.
Jan
static void strcpytoW(WCHAR *dst, const char *src)
{
WCHAR c;
while ((c=*src++)!=0) *dst++=c;
*dst=0;
}
/*************************************/
typedef DWORD (WINAPI *FN_NUMCONSOLEFONT)();
FN_NUMCONSOLEFONT GetNumberOfConsoleFonts;
static bool setfont(const HANDLE h, char *facename, CONSOLE_FONT_INFOEX &in,CONSOLE_FONT_INFOEX &out)
{
in.cbSize=out.cbSize=sizeof(in);
strcpytoW(in.FaceName,facename);
if (!SetCurrentConsoleFontEx(h,FALSE,&in)) return false;
HMODULE hm = ::GetModuleHandleA("KERNEL32.DLL");
if (!hm) return false;
GetNumberOfConsoleFonts = (FN_NUMCONSOLEFONT) GetProcAddress(hm, "GetNumberOfConsoleFonts");
if (!GetNumberOfConsoleFonts) return false;
DWORD numConsoleFonts=GetNumberOfConsoleFonts();
if (!GetCurrentConsoleFontEx(h,FALSE,&out)) return false;
return out.nFont>=0 && out.nFont<numConsoleFonts && _wcsicmp(in.FaceName,out.FaceName)==0;
}
/*************************************/
static char *set_console(char *facename,int page)
{
HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_FONT_INFOEX fi={sizeof(fi),-1,{0,0},54,700},newfi;
CONSOLE_FONT_INFO curfi;
if (!GetCurrentConsoleFont(hStdout,FALSE,&curfi)) return NULL;
for (fi.dwFontSize=curfi.dwFontSize;;fi.dwFontSize.Y++)
{
if (!setfont(hStdout,facename,fi,newfi))
{
facename="Lucida Console";
if (!setfont(hStdout,facename,fi,newfi)) return NULL;
}
if (newfi.dwFontSize.X>=curfi.dwFontSize.X) break;
}
if (!SetConsoleOutputCP(page)) return NULL;
return facename;
}
/*************************************/
int main(int argc, char *argv[])
{
char *faceused;
if ((faceused=set_console("Consolas",1252))!=NULL)
printf("Console succesful, using %s\n",faceused);
else puts("Console failed");
puts("\x86 \x87 \xa7 \xa9 \xae \xb1 \xbc");
Press Ctrl-F5 to start executing the program. Before it returns to VS, it says "Press any key...". This gives you the opportunity to click on the window's upper left corner, properties and change the font to raster fonts there, too.

Resources