473,406 Members | 2,894 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

CallBack

Hello All,
I am trying to integrate the ftpclient (in ftplib) with an application that
has been written I C/C++.
I am able to delete,send change working directories etc. When I receive data
back from the server i.e. Retrieve files, get a directory listing) I want to
pass a callback function to the ftpclient so that the output does not get
displayed on stdout. The code I have written is as shown below. Can anyone
out there give me advise on how to write "c_function" that is called in
ftpTest::DoesFileExist ?Any help will be most appreciated. The code runs
under windows and is compiled with VC++ 6.0. THe code is as shown below.
Regards,
Ram
>>>>>>>>>>>>>>> CODE START >>>>>>>>>>>>>>>>>>> // test.cpp : Defines the entry point for the console application.
//
#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
#include <direct.h>
#include "C:\Python24\include\Python.h"

Class ftpTest
{
Private:
PyObject *_FTP;
PyObject *_callBack;

Public:
FtpTest (const char * ipAddress, const char * username, const char *
password); // Open the FTP connection, Initialize python interpreter
~ftpTest (); // Close connection and finalize python interpreter
Int Close ();
Int GetFile (const char * fileName);
Int PutFile (const char * fileName);
Int ChangeDir (const char * dirName);
Int DoesFileExist (const char * fileName);
Int DeleteFile (const char * fileName);
Int ChangePermissions (const char * fileName, const char * permissions);
};

FtpTest::ftpTest (const char * ipAddress, const char * username, const char
* password)
{
PyObject *modname, *mod, *func, *mdict, *pArgs, *pValue;

// Initialize the python interpreter
Py_Initialize();

Modname = PyString_FromString("ftplib");

If (modname == NULL)
{
// Log an error
}
Else
{
Mod = PyImport_Import (modname);
}

If (mod)
{
Mdict = PyModule_GetDict (mod);
Func = PyDict_GetItemString (mdict, "FTP");
If (func)
{
Printf ("I am here -- 1\n");
}

Const char *arguments[] = {ipAddress, username, password};

PArgs = PyTuple_New (3);
For (int I = 0; I < 3; ++I)
{
PValue = PyString_FromString(arguments[i]);
If (!pValue)
{
Py_DECREF(pArgs);
Py_DECREF(mod);
}
/* pValue reference stolen here: */
PyTuple_SetItem(pArgs, I, pValue);
}

_FTP = PyObject_CallObject (func, pArgs);

If (_FTP != NULL)
{
Printf ("I am here -- 2\n");
}
}
}

FtpTest::~ftpTest ()
{
//Py_Finalize();
}

Int ftpTest::Close ()
{
Return (1);
}

Int ftpTest::ChangeDir (const char * dirName)
{
PyObject *t = PyObject_CallMethod (_FTP, "cwd", "(S)", dirName);
Return (1);
}

Int ftpTest::DeleteFile (const char * fileName)
{
PyObject *t = PyObject_CallMethod (_FTP, "delete", "(S)", fileName);
Return (1);
}

Int ftpTest::PutFile (const char * fileName)
{
PyObject * py_file = PyFile_FromString ((char *)fileName, "rb");
Std::string S("STOR ");
s += fileName;
PyObject * t = PyObject_CallMethod (_FTP, "storlines", "(sO)", S.c_str(),
py_file);
Return (1);
}

Int ftpTest::ChangePermissions (const char * fileName, const char *
permissions)
{
Std::string S("SITE");
s += " chmod ";
s += permissions;
s += " ";
s += fileName;
PyObject * t = PyObject_CallMethod (_FTP, "sendcmd", "(S)", S.c_str());
Return (1);
}

Int ftpTest::GetFile (const char * fileName)
{
// TODO: Implement callback
#if 0
PyObject *t = PyObject_CallMethod (_FTP, "retrlines", "(S)", "RETR ram.txt
);
#endif
Return (1);
}

PyObject * c_function(char * data)
{
// Process the data received in "data"
Return (NULL);
}

Int ftpTest::DoesFileExist (const char * fileName)
{
// Just get the list
PyObject *t = PyObject_CallMethod (_FTP, "retrlines", "(sO)", "LIST",
c_function);
If (t == NULL)
{
Printf ("I am here -- 4\n");
}
Return (1);
}

Int main ()
{
FtpTest ftpClient("127.0.0.1", "xxx", "yyyy");
FtpClient.DoesFileExist(NULL);
Return (1);
}
>>>>>>>>>>>> CODE END >>>>>>>>>>>>>>>>>>>>>

Mar 4 '06 #1
0 1000

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: scott ocamb | last post by:
hello I have implemented a solution using async methods. There is one async method that can be invoked multiple times, ie there are multiple async "threads" running at a time. When these...
0
by: vijaya | last post by:
I've to invoke a unmanaged dll fucntion in C# which uses a callback fucntion.The unmanaged dll fucntion is as follows **************************************** The Original Fucntion in the dll...
4
by: ma740988 | last post by:
// file sltest.h #ifndef SLTEST_H #define SLTEST_H class CallbackBase // herb shutters gotW source .. { public: virtual void operator()() const { }; virtual ~CallbackBase() = 0; };
15
by: Felix Kater | last post by:
Hi, in a given library I register callback functions with this function: bool set_callback(int index, int (*callback_function)(long)); I need the callback function to also pass the index...
2
by: MR | last post by:
help! I have an unmanaged DLL that I do not have the source code, so i can't recompile or make changes. the DLL requires a callback function. I would like to implement the callback method in a...
6
by: smmk25 | last post by:
Before I state the problem, I just want to let the readers know, I am knew to C++\CLI and interop so please forgive any newbie questions. I have a huge C library which I want to be able to use in...
2
by: Evan Burkitt | last post by:
Hi, all. I have a Windows DLL that exports a number of functions. These functions expect to receive a pointer to a callback function and an opaque void* parameter. The callback functions are...
10
by: SQACPP | last post by:
Hi, I try to figure out how to use Callback procedure in a C++ form project The following code *work* perfectly on a console project #include "Windows.h" BOOL CALLBACK...
0
by: Tim Spens | last post by:
--- On Fri, 6/27/08, Tim Spens <t_spens@yahoo.comwrote: I think I know where the problem is but I'm unsure how to fix it. When I call Register_Handler(...) from python via...
5
by: Jef Driesen | last post by:
I have a C DLL that I want to use from a C# project. The C header file contains these declarations: typedef void (*callback_t) (const unsigned char *data, unsigned int size, void *userdata);...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.