how to pass a string to a c function Topic is solved

If you are using wxDev-C++ for your wxWidgets design, please ask your questions here instead of in IDE Related.
Post Reply
ding
Experienced Solver
Experienced Solver
Posts: 70
Joined: Wed Jan 04, 2006 7:55 am
Location: Singapore

how to pass a string to a c function

Post by ding »

I have try passing a wxString path to a c function locally, but it won't work. If i try passing it globally it will work. How do i pass a wxString to a c function.
At the top, that how i declare,

void extract(wxString path);

void testFrm::WxToolButton1Click(wxCommandEvent& event)
{

if (WxOpenFileDialog1->ShowModal() == wxID_OK)
{
path = WxOpenFileDialog1->GetPath();
wxMessageBox(path);
extract(path);
}

}

void extract(wxString path)
{
...
}


So any error with that?
wxuser
Knows some wx things
Knows some wx things
Posts: 29
Joined: Fri Feb 25, 2005 5:45 am
Location: India

Post by wxuser »

Your code seems to be ok, i dont see any problem with that.

If your issue is getting a string out of wxString, then use c_str()

Code: Select all

void extract(wxString path)
{
const char* c_string = path.c_str();
...
} 
or just pass c_string into the function

then use c_string in whatever you want it for.
leio
Can't get richer than this
Can't get richer than this
Posts: 802
Joined: Mon Dec 27, 2004 10:46 am
Location: Estonia, Tallinn
Contact:

Post by leio »

c_str() is only suitable to use for getting a wxChar out of a wxString. That means char* OR a wchar_t* based on the build type.

(const char*)mb_str(conv_class) is for always getting a char* regardless of wx build type.

(const wchar_t*)wc_str(...) is for always getting a wchar_t* out of a wxString regardless of wx build type.

extract isn't a C function if it's in the .cpp file. You are not wrapping it inside extern "C". It is taking a wxString there, and that's an instance. However, you are making a copy there, which might be unnecessary, and usually is. I suggest looking up what references are in C++.
Compilers: gcc-3.3.6, gcc-3.4.5, gcc-4.0.2, gcc-4.1.0 and MSVC6
OS's: Gentoo Linux, WinXP; WX: CVS HEAD

Project Manager of wxMUD - http://wxmud.sf.net/
Developer of wxGTK;
gtk+ port maintainer of OMGUI - http://www.omgui.org/
ding
Experienced Solver
Experienced Solver
Posts: 70
Joined: Wed Jan 04, 2006 7:55 am
Location: Singapore

Post by ding »

Thanks for helping, look like i have to pass it globally. To be exact, extract() is called a function prototype if i am not wrong. That the very basic c use to pass variable to a function but it not working at wxwidget frame.
leio
Can't get richer than this
Can't get richer than this
Posts: 802
Joined: Mon Dec 27, 2004 10:46 am
Location: Estonia, Tallinn
Contact:

Post by leio »

I don't understand what problem you are having.
The code seems generally fine. I'd certainly make the wxString argument of extract a reference, of course.
I also do not see any C involved here, other than the fact that extract is a function (that takes a C++ class instance as an argument, but that's basically like a struct), and not a static method of a class.
Can you explain what problem are you having, if any? Any compiler error, anything?
Compilers: gcc-3.3.6, gcc-3.4.5, gcc-4.0.2, gcc-4.1.0 and MSVC6
OS's: Gentoo Linux, WinXP; WX: CVS HEAD

Project Manager of wxMUD - http://wxmud.sf.net/
Developer of wxGTK;
gtk+ port maintainer of OMGUI - http://www.omgui.org/
lowjoel
Part Of The Furniture
Part Of The Furniture
Posts: 1511
Joined: Sun Jun 19, 2005 11:37 am
Location: Singapore
Contact:

Post by lowjoel »

yea, you should explain what error you are getting, not expecting us to read your code and understand exactly what the error is (with no clue what we are looking for).
ding
Experienced Solver
Experienced Solver
Posts: 70
Joined: Wed Jan 04, 2006 7:55 am
Location: Singapore

Post by ding »

Thanks for helping! I realise that I made a mistake by declaring the function prototype at the wrong place. This is how it should be place. Thanks for all your help.

..........
#include "testFrm.h"

#define filewritten "rmsd.txt"
#define filewritten1 "mean.txt"

long checkrunsample(long *,long *,wxString path);
void extract(wxString path);

//Do not add custom headers
//wxDev-C++ designer will remove them
////Header Include Start
#include "WxToolButton3_XPM.xpm"
#include "WxToolButton2_XPM.xpm"
#include "WxToolButton1_XPM.xpm"

////Header Include End

//----------------------------------------------------------------------------
// testFrm
//----------------------------------------------------------------------------
//Add Custom Events only in the appropriate block.
//Code added in other places will be removed by wxDev-C++
////Event Table Start
..............

After declaring the function below #include "testFrm.h" everything is working perfectly. Before i declare a function before #include "testFrm.h", it give a lot of error. Anyway thanks for telling me to look at the error. Probably, I was too tired at the time. Sorry.
mathgl
In need of some credit
In need of some credit
Posts: 2
Joined: Mon Mar 06, 2006 4:56 pm
Location: China

or you may use <string> instead

Post by mathgl »

it is part of STL
i think it probably may resolve your problem
Post Reply