To declare a CString function inside a header file - visual-c++-2008

How to properly declare this function inside this header file?
CString ExecuteExternalProgram(CString pictureName);
#pragma warning( disable: 4049 ) /* more than 64k source lines */
#ifndef __REQUIRED_RPCNDR_H_VERSION__
#define __REQUIRED_RPCNDR_H_VERSION__ 475
#endif
#include "rpc.h"
#include "rpcndr.h"
CString ExecuteExternalProgram(CString pictureName); //<---- THIS LINE IS ERROR?
#ifndef __RPCNDR_H_VERSION__
#error this stub requires an updated version of <rpcndr.h>
#endif // __RPCNDR_H_VERSION__
#ifndef __Example1_h__
#define __Example1_h__
#if defined(_MSC_VER) && (_MSC_VER >= 1020)
#pragma once
#endif
/* Forward Declarations */
#ifdef __cplusplus
extern "C"{
#endif
#ifndef __Example1_INTERFACE_DEFINED__
#define __Example1_INTERFACE_DEFINED__
void Output(
/* [string][in] */ const char *szOutput);
extern handle_t hExample1Binding;
extern RPC_IF_HANDLE Example1_v1_0_c_ifspec;
extern RPC_IF_HANDLE Example1_v1_0_s_ifspec;
#endif /* __Example1_INTERFACE_DEFINED__ */
#ifdef __cplusplus
}
#endif
#endif

I created an Empty Project in Visual Studio and I didn't have any problem compiling:
#include <atlstr.h>
#include "Example1.h" // Direct copy of your code from above
int main(int argc, char **argv) {
return 0;
}
I was tipped off about atlstr.h from http://www.codeguru.com/forum/showthread.php?t=231164.
Is that what you mean by "properly" declare?

Related

How to encapsulate library handle in Perl XS

I wanted to send/receive MQTT messages from Perl. For various reasons (MQTT 5 support, TLS) I don't want to use existing Perl libraries. So I tried to create XS bindings to Paho MQTT C Library. I somehow adapted provided example to link Perl module to Paho library using relly basic Perl XS:
#define PERL_NO_GET_CONTEXT
#include "EXTERN.h"
#include "perl.h"
#include "XSUB.h"
#include "ppport.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <MQTTClient.h>
#define CLIENTID "ExampleClientPub"
#define QOS 1
#define TIMEOUT 10000L
MODULE = paho PACKAGE = paho
int
mqtt_connect_and_send (server_address, username, topic, payload)
char * server_address
char * username
char * topic
char * payload
CODE:
MQTTClient client;
MQTTClient_connectOptions conn_opts = MQTTClient_connectOptions_initializer;
MQTTClient_message msg = MQTTClient_message_initializer;
MQTTClient_deliveryToken token;
int rc;
/* connect to server */
MQTTClient_create(&client, server_address, CLIENTID,
MQTTCLIENT_PERSISTENCE_NONE, NULL);
conn_opts.keepAliveInterval = 20;
conn_opts.cleansession = 1;
conn_opts.username = username;
if ((rc = MQTTClient_connect(client, &conn_opts)) != MQTTCLIENT_SUCCESS)
{
/* didn't connect */
die("Failed to connect, return code %d", rc);
}
/* fill in message data and send it */
msg.payload = payload;
msg.payloadlen = strlen(payload);
msg.qos = QOS;
msg.retained = 0;
MQTTClient_publishMessage(client, topic, &msg, &token);
rc = MQTTClient_waitForCompletion(client, token, TIMEOUT);
/* shutdown connection */
MQTTClient_disconnect(client, 10000);
MQTTClient_destroy(&client);
if (rc != MQTTCLIENT_SUCCESS) {
/* didn't send the message */
die("Failed to send message, return code %d", rc);
}
RETVAL = 1;
OUTPUT:
RETVAL
This is working OK. But now I want to split function mqtt_connect_and_send to 3 functions: mqtt_connect, mqtt_send_message, mqtt_disconnect. And my question is - how to do this? How to create a handle (client in my case) in XS in one function, return it to Perl to somehow store it in a scalar and use that handle in ahother XS function to be used to send more messages? I want to be able to do this in Perl:
my $client = paho::mqtt_connect($server_spec, $username, $password, $more_opts);
$success = paho::mqtt_send($client, $data, $message_opts);
# ... more of mqtt_send's
paho::mqtt_disconnect($server)
I tried to set RETVAL RETVAL = &client or mXPUSHu(&client) but that I didn't get anywhere with that. Can you point me to some example how to get client to Perl and then back to XS to be used again?
Thank you.
Here is an example of how you can return the client as a perl object:
#define PERL_NO_GET_CONTEXT
#include "EXTERN.h"
#include "perl.h"
#include "XSUB.h"
#include "ppport.h" // allow the module to be built using older versions of Perl
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <MQTTClient.h>
#define CLIENTID "ExampleClientPub"
#define QOS 1
#define TIMEOUT 10000L
UV get_hash_uv(HV *hash, const char *key) {
#define get_hash_uv(a,b) get_hash_uv(aTHX_ a,b)
SV * key_sv = newSVpv (key, strlen (key));
UV value;
if (hv_exists_ent (hash, key_sv, 0)) {
HE *he = hv_fetch_ent (hash, key_sv, 0, 0);
SV *val = HeVAL (he);
STRLEN val_length;
char * val_pv = SvPV (val, val_length);
if (SvIOK (val)) {
value = SvUV (val);
}
else {
croak("Value of hash key '%s' is not a number", key);
}
}
else {
croak("The hash key for '%s' doesn't exist", key);
}
return value;
}
MODULE = Paho PACKAGE = Paho
PROTOTYPES: DISABLE
SV *
mqtt_connect(server_address, username)
char *server_address
char *username
CODE:
int rc;
MQTTClient client; // void *
MQTTClient_connectOptions conn_opts = MQTTClient_connectOptions_initializer;
MQTTClient_create(&client, server_address, CLIENTID,
MQTTCLIENT_PERSISTENCE_NONE, NULL);
conn_opts.keepAliveInterval = 20;
conn_opts.cleansession = 1;
conn_opts.username = username;
if ((rc = MQTTClient_connect(client, &conn_opts)) != MQTTCLIENT_SUCCESS)
{
MQTTClient_destroy(&client);
croak("Failed to connect, return code %d", rc);
}
HV *hash = newHV();
SV *self = newRV_noinc( (SV *)hash );
SV *sv = newSVuv(PTR2IV(client));
hv_store (hash, "client", strlen ("client"), sv, 0);
RETVAL = sv_bless(self, gv_stashpv( "Paho::Client", GV_ADD ) );
OUTPUT:
RETVAL
MODULE = Paho PACKAGE = Paho::Client
void
DESTROY(self)
SV *self
CODE:
MQTTClient client; // void *
HV *hv = (HV *) SvRV(self);
UV addr = get_hash_uv(hv, "client");
client = (MQTTClient ) INT2PTR(SV*, addr);
MQTTClient_destroy(&client);
printf("Paho::Client destroy\n");
I am not able to test this yet, because I do not have good values for the input parameters server_address and username. Please provide data that we can test with.
There's no point in building a hash unless you want the class to be extensible.[1] As such, Håkon Hægland's solution can be simplified by returning a scalar-based object. Doing so is quite common for XS-based classes.
#define PERL_NO_GET_CONTEXT
#include "EXTERN.h"
#include "perl.h"
#include "XSUB.h"
#include "ppport.h" // allow the module to be built using older versions of Perl
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <MQTTClient.h>
#define CLIENTID "ExampleClientPub"
#define QOS 1
#define TIMEOUT 10000L
MODULE = paho PACKAGE = paho
PROTOTYPES: DISABLE
SV *
mqtt_connect(server_address, username)
char *server_address
char *username
CODE:
int rc;
MQTTClient client; // void *
MQTTClient_connectOptions conn_opts = MQTTClient_connectOptions_initializer;
MQTTClient_create(&client, server_address, CLIENTID,
MQTTCLIENT_PERSISTENCE_NONE, NULL);
conn_opts.keepAliveInterval = 20;
conn_opts.cleansession = 1;
conn_opts.username = username;
if ((rc = MQTTClient_connect(client, &conn_opts)) != MQTTCLIENT_SUCCESS)
{
MQTTClient_destroy(&client);
croak("Failed to connect, return code %d", rc);
}
SV *sv = newSVuv(PTR2IV(client));
SV *self = newRV_noinc(sv);
RETVAL = sv_bless(self, gv_stashpv("Paho::Client", GV_ADD));
OUTPUT:
RETVAL
void
DESTROY(self)
SV *self
CODE:
MQTTClient client; // void *
client = INT2PTR(MQTTClient, SvUV(SvRV(self)));
MQTTClient_destroy(&client);
printf("Paho::Client destroy\n");
It can still be extended using the inside-out object technique. And of course, it can still be wrapped.

GTK basics : learning from sources

I am new at programming Gtk gnome linux C application, and I am trying to learn from some source reagind and online gnome documentation but I can't do it because this is in my opinion very poor and bad. I have a problem understanding this following source: after creating a GObject of GApplication type, how can this main function call other parts of programs? (This is from gedit source) I learnt that GApplication doesn't include any pointer to some code.
/*
* gedit.c
* This file is part of gedit
*
* Copyright (C) 2005 - Paolo Maggi
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, see <http://www.gnu.org/licenses/>.
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include "gedit-app.h"
#ifdef OS_OSX
#include "gedit-app-osx.h"
#else
#ifdef G_OS_WIN32
#include "gedit-app-win32.h"
#else
#include "gedit-app-x11.h"
#endif
#endif
#include <glib.h>
#include <locale.h>
#include <libintl.h>
#include "gedit-dirs.h"
#include "gedit-debug.h"
#ifdef G_OS_WIN32
#include <gmodule.h>
static GModule *libgedit_dll = NULL;
/* This code must live in gedit.exe, not in libgedit.dll, since the whole
* point is to find and load libgedit.dll.
*/
static gboolean
gedit_w32_load_private_dll (void)
{
gchar *dllpath;
gchar *prefix;
prefix = g_win32_get_package_installation_directory_of_module (NULL);
if (prefix != NULL)
{
/* Instead of g_module_open () it may be possible to do any of the
* following:
* A) Change PATH to "${dllpath}/lib/gedit;$PATH"
* B) Call SetDllDirectory ("${dllpath}/lib/gedit")
* C) Call AddDllDirectory ("${dllpath}/lib/gedit")
* But since we only have one library, and its name is known, may as well
* use gmodule.
*/
dllpath = g_build_filename (prefix, "lib", "gedit", "libgedit.dll", NULL);
g_free (prefix);
libgedit_dll = g_module_open (dllpath, 0);
if (libgedit_dll == NULL)
{
g_printerr ("Failed to load '%s': %s\n",
dllpath, g_module_error ());
}
g_free (dllpath);
}
if (libgedit_dll == NULL)
{
libgedit_dll = g_module_open ("libgedit.dll", 0);
if (libgedit_dll == NULL)
{
g_printerr ("Failed to load 'libgedit.dll': %s\n",
g_module_error ());
}
}
return (libgedit_dll != NULL);
}
static void
gedit_w32_unload_private_dll (void)
{
if (libgedit_dll)
{
g_module_close (libgedit_dll);
libgedit_dll = NULL;
}
}
#endif
int
main (int argc, char *argv[])
{
GType type;
GeditApp *app;
gint status;
const gchar *dir;
#ifdef OS_OSX
type = GEDIT_TYPE_APP_OSX;
#else
#ifdef G_OS_WIN32
if (!gedit_w32_load_private_dll ())
{
return 1;
}
type = GEDIT_TYPE_APP_WIN32;
#else
type = GEDIT_TYPE_APP_X11;
#endif
#endif
/* NOTE: we should not make any calls to the gedit api before the
* private library is loaded */
gedit_dirs_init ();
/* Setup locale/gettext */
setlocale (LC_ALL, "");
dir = gedit_dirs_get_gedit_locale_dir ();
bindtextdomain (GETTEXT_PACKAGE, dir);
bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
textdomain (GETTEXT_PACKAGE);
app = g_object_new (type,
"application-id", "org.gnome.gedit",
"flags", G_APPLICATION_HANDLES_COMMAND_LINE | G_APPLICATION_HANDLES_OPEN,
NULL);
status = g_application_run (G_APPLICATION (app), argc, argv);
/* Break reference cycles caused by the PeasExtensionSet
* for GeditAppActivatable which holds a ref on the GeditApp
*/
g_object_run_dispose (G_OBJECT (app));
g_object_add_weak_pointer (G_OBJECT (app), (gpointer *) &app);
g_object_unref (app);
if (app != NULL)
{
gedit_debug_message (DEBUG_APP, "Leaking with %i refs",
G_OBJECT (app)->ref_count);
}
#ifdef G_OS_WIN32
gedit_w32_unload_private_dll ();
#endif
return status;
}
/* ex:set ts=8 noet: */

Why does S-Function only execute mdlInitializeSizes() and does not execute mdlOutputs(), mdlStart() etc?

#define S_FUNCTION_NAME myfunction_sFun
#define S_FUNCTION_LEVEL 2
#define MDL_INITIAL_SIZES
#define MDL_INITIALIZE_SAMPLE_TIMES
#include "tmwtypes.h"
#include "simstruc_types.h"
#include "mex.h"
#include "simstruc.h"
void mdlInitializeSizes(SimStruct *S)
{
ssPrintf("Initialize\n");
//My code has been removed from here
ssPrintf("End Initialize\n");
}
void mdlInitializeSampleTimes(SimStruct *S)
{
ssPrintf("Sample Times\n");
}
#define MDL_OUTPUTS
#ifdef MDL_OUTPUTS
void mdlOutputs(SimStruct *S, int_T tid)
{
ssPrintf("Outputs\n");
}
#endif
#define MDL_START
#ifdef MDL_START
void mdlStart(SimStruct *S)
{
ssPrintf("Start\n");
}
#endif
void mdlTerminate(SimStruct *S){}
/=============================
Required S-function trailer *
=============================/
#ifdef MATLAB_MEX_FILE /* Is this file being compiled as a MEX-file? */
#include "simulink.c" /* MEX-file interface mechanism */
#else
#include "cg_sfun.h" /* Code generation registration function */
#endif
mdlStart and mdlOutputs are option methods that only get called in certain circumstances. For instance, mdlOutputs only gets called if there are any outputs defined - which in your case there aren't. (I am a little surprised that mdlStart is not being called.)
See the documentation Simulink Engine Interaction with C S-Functions for which methods are optional, and their calling sequence.

how to call a function of c++ file in .m file?

I have a header file test.h
#ifndef __visibilty_test__test__
#define __visibilty_test__test__
#include <iostream>
using namespace std;
class test{
public:
void print(string s);
};
#endif
and test.mm
#include "test.h"
using namespace std;
void test:: print(string s){
cout << s << endl;
}
now I want to call print function in my AppDelegate.m file in an iOS Application. Can anyone help me?
Thanks in advance
Rename AppDelegate.m to AppDelegate.mm.
Call the method as you would in C++:
test t;
t.print("Hello");
Your object:
#include <iostream>
using namespace std;
class test
{
public:
void print(string s)
{
cout << s << endl;
}
};
call from mm file
test *t = new test;
t->print("hello");

How High the Pin of Parallel Port

/* Necessary includes for drivers */
#include <linux/init.h>
//#include <linux/config.h>
#include <linux/module.h>
#include <linux/kernel.h> /* printk() */
#include <linux/slab.h> /* kmalloc() */
#include <linux/fs.h> /* everything... */
#include <linux/errno.h> /* error codes */
#include <linux/types.h> /* size_t */
#include <linux/proc_fs.h>
#include <linux/fcntl.h> /* O_ACCMODE */
#include <linux/ioport.h>
#include <asm/system.h> /* cli(), *_flags */
#include <asm/uaccess.h> /* copy_from/to_user */
#include <asm/io.h> /* inb, outb */
MODULE_LICENSE("Dual BSD/GPL");
MODULE_AUTHOR("Nikunj");
/* Function declaration of parlelport.c */
int parlelport_open(struct inode *inode, struct file *filp);
int parlelport_release(struct inode *inode, struct file *filp);
ssize_t parlelport_read(struct file *filp, char *buf, size_t count, loff_t *f_pos);
ssize_t parlelport_write(struct file *filp, char *buf, size_t count, loff_t *f_pos);
void parlelport_exit(void);
int parlelport_init(void);
/* Structure that declares the common */
/* file access fcuntions */
struct file_operations parlelport_fops = {
read : parlelport_read,
write : parlelport_write,
open : parlelport_open,
release : parlelport_release
};
/* Driver global variables */
/* Major number */
int parlelport_major = 61;
/* Control variable for memory */
/* reservation of the parallel port*/
int port;
module_init(parlelport_init);
module_exit(parlelport_exit);
int parlelport_init(void)
{
int result;
/* Registering device */
result = register_chrdev(parlelport_major, "parlelport", &parlelport_fops);
if (result < 0)
{
printk("<1>parlelport: cannot obtain major number %d\n",parlelport_major);
return result;
}
/* Registering port */
port = check_region(0x378, 1);
if (port)
{
printk("<1>parlelport: cannot reserve 0x378\n");
result = port;
goto fail;
}
request_region(0x378, 1, "parlelport");
printk("<1>Inserting parlelport module\n");
return 0;
fail:
parlelport_exit();
return result;
}
void parlelport_exit(void)
{
/* Make major number free! */
unregister_chrdev(parlelport_major, "parlelport");
/* Make port free! */
if (!port)
{
release_region(0x378,1);
}
printk("<1>Removing parlelport module\n");
}
int parlelport_open(struct inode *inode, struct file *filp)
{
/* Success */
return 0;
}
int parlelport_release(struct inode *inode, struct file *filp)
{
/* Success */
return 0;
}
ssize_t parlelport_read(struct file *filp, char *buf, size_t count, loff_t *f_pos)
{
/* Buffer to read the device */
char parlelport_buffer;
/* Reading port */
parlelport_buffer = inb(0x378);
/* We transfer data to user space */
copy_to_user(buf,&parlelport_buffer,1);
/* We change the reading position as best suits */
if (*f_pos == 0)
{
*f_pos+=1;
return 1;
}
else
{
return 0;
}
}
ssize_t parlelport_write( struct file *filp, char *buf, size_t count, loff_t *f_pos)
{
char *tmp;
/* Buffer writing to the device */
char parlelport_buffer;
tmp=buf+count-1;
copy_from_user(&parlelport_buffer,tmp,1);
/* Writing to the port */
outb(parlelport_buffer,0x378);
return 1;
}
this is the code of parallel port device driver and this is my first c code for that.
Please help me to solve the below problem
i have successfully compile the code and create the .ko file successfully and successfully load in the ubuntu 9.10 OS but thee is no high or low the pin so please help me
How you are checking answer?? After installed module do
dmesg
Actually you can not acquire region at 0x378. Because by default system allocate this one to parport0.
Do cat /proc/ioports so you will find that at 0x378 there is parport0.
You can directly write on that address (0x378), without using that checking and requesting.
My suggestion is that don't directly go in complex form. In write function just write
outb(0x378,1) and check (by inserting hardware having LEDs) LED on/off on data pins.