|
I have a form called form1.h, and respectively form1.cpp. I have the following code:
#include "stdafx.h"
#include "Form1.h"
#include <windows.h>
#include <iostream>
#include <fstream>
using namespace fyp1;
int APIENTRY _tWinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPTSTR lpCmdLine,
int nCmdShow)
{
System::Threading::Thread::CurrentThread->ApartmentState = System::Threading::ApartmentState::STA;
ifstream iFile("test.txt");
Application::Run(new Form1());
return 0;
}
I wanna add ifstream into above so that I can read-in a file, then pass the parameter into Form1(). But how can I do that? (ignore the button problem, i don't need it now)The above code cannot be compiled.
Thanks again.
-- modified at 15:09 Thursday 4th May, 2006
|
|
|
|
|
As i told you, you will need StreamReader. It is .NET class, not MFC or other, so there you musn't include iostream, fstream and windows.h. Include file Windows.h often couses compile problem in .Net framework project. so remove all these include
you will Need to add in form1.h file not in form1.cpp.
using namespace System::IO;
public ref class Form1: public System::Windows::Forms::Form
{
public:
Form1(void) ...
...
Private: StreamReader ^streamFile;
Private: String ^streamReadLine;
...
public: ReadFile(void)
{
streamFile = File::OpenText("test.txt");
while (streamFile->EndOfStream != true)
{
streamReadLine = streamFile->ReadLine();
}
}
};
If you still don't know, at least tell me witch .net framework you are using.
|
|
|
|
|
I maintain a Visual C++ Version 6 app. It takes data structures from an Excel app, processes results, and returns structures to Excel. I must now write a C++ .Net wrapper, to take datasets from a VB.Net calling app, create structures from the datasets, send the structures to the Visual C++ Version 6 app, get the return structures, then create and return a dataset to the VB.Net calling app.
I have not been able to expose a public function in the C++ .Net wrapper.
The C++ .Net header code is:
using namespace System;
using namespace System::Data;
using namespace System::Runtime::InteropServices;
namespace TK_DLL_Header {
public class TK_DLL
{
public:
// constructor
TK_DLL();
// destructor
~TK_DLL();
unsigned short ProcessResults (DataSet dsStudyParm, DataRow drFinParms);
};
}
The C++ .Net .cpp code is:
#include "StdAfx.h"
#include "TK_CPlusPlus_Class_Library.h"
using namespace System;
using namespace System::Runtime::InteropServices;
const unsigned short TK_TRUE = 1;
const unsigned short TK_FALSE = 0;
//#define BDS_TK_API __declspec(dllexport)
using namespace TK_DLL_Header;
//namespace TK_DLL_Header {
unsigned short TK_DLL::ProcessResults(System::Data::DataSet dsStudyParm, System::Data::DataRow drFinParms)
{
BDS_ClearAggreg();
return TK_TRUE;
}
[DllImport("BDS_TK.dll", EntryPoint="BDS_ClearAggreg")]
short BDS_ClearAggreg();
I have attempted to access the ProcessResults function in a VB.Net app, in two ways. First, using intellisense:
‘ this statement works
Dim tk As New TK_DLL_Header.TK_DLL
‘ this statement does not work; ProcessResults is not in the list of items after tk.
result = tk.ProcessResults(dsStudyParm, drFinParms)
tk.ProcessResults has a blue line under it; a mouse-over message says “ProcessResults is not a member of TK_DLL_Header.TK_DLL”
In the same VB.Net app, I have added the following declaration at the top of the file:
Private Declare Function ProcessResults Lib "TK_CPlusPlus_Class_Library.dll" (ByRef dsStudyParm As DataSet, ByRef drFinParms As DataRow) As Integer
Using the following call:
result = ProcessResults(dsStudyParm, drFinParms)
Raises the error message:
“Unable to find an entry point named ‘ProcessResults’ in DLL ‘TK_CPlusPlus_Class_Library.dll’
I am missing something very fundamental here. If anyone can help, re how to expose a public function from VC++ .Net, it would be greatly appreciated.
Thanks.
ppanke
|
|
|
|
|
Your assumption is very correct that you are missing something very fundamental here .
The class you declared is not a managed class. it is an unmanaged. To make it a managed type change the class declaration by adding the modifer ref (for .Net 2.0) or __gc (for .Net 1.1) .
so the new class declaration will be:
.net 1.1<br />
namespace TK_DLL_Header {<br />
<br />
public __gc class TK_DLL<br />
{<br />
public: <br />
TK_DLL();<br />
~TK_DLL();<br />
<br />
unsigned short ProcessResults (DataSet dsStudyParm, DataRow drFinParms);<br />
};<br />
}<br />
<br />
.net 2.0:<br />
<br />
namespace TK_DLL_Header {<br />
<br />
public ref class TK_DLL<br />
{<br />
public: <br />
TK_DLL();<br />
~TK_DLL();<br />
<br />
unsigned short ProcessResults (DataSet dsStudyParm, DataRow drFinParms);<br />
};<br />
}
cheers..mil10
|
|
|
|
|
Thanks, but when I added the ref keyword, the class would not compile (it compiles fine without ref). I got an "unresolved externals" error. I do not know why.
P. Panke
|
|
|
|
|
what is he unresoleved external sybal. I meant which function is unresolved ?
|
|
|
|
|
I want to use the timeSetEvent in the C++/CLI for a 1msec timing resolution.
I seem to have a problem in the CALLBACK function and when i try to replace it with a delegate i have other problems.
Can anyone show me the way to use timeSetEvent with a CALLBACK/Delegate function that actually work?
|
|
|
|
|
SampleForm::SampleForm()
{
InitForm();
}
void SampleForm::InitForm()
{
Timer* timer1;
timer1 = new Timer();
timer1->Interval = 3000; // 3 seconds resolution
timer1->add_Tick(new EventHandler(this,OnTimer));
timer1->Start();
}
void SampleForm::OnTimer(System::Object* obj,System::EventArgs* ea) //declare as static method in .h file
{
}
^-^
@|@
- redCat
|
|
|
|
|
this is now the timeSetEvent, but the Timer class.
The Timer class accuracy is very poor, unlike the timeSetEvent.
Can anybody show me how to use it in c++/cli .net 2005?
|
|
|
|
|
First of all, you should note that accuracy is not the same as precision[^]. 10ms is pretty much the most accurate you can get with Win32, without screwing with the operating system metrics or reinstalling the HAL.
But anyway, you use timeSetEvent in C++/CLI the same way you use it in Win32.
#include <windows.h>
#pragma comment(lib, "winmm")
public ref class TimerWrapper
{
EventHandle ^eventHandle;
public:
TimerWrapper()
: eventHandle(false, EventResetMode::AutoReset)
{
}
int TimerFunc()
{
for( ; ; )
{
eventHandle->WaitOne();
...
}
}
void UseTimeEvent()
{
timeSetEvent(100, 55, reinterpret_cast<LPTIMECALLBACK>(
eventHandle->SafeWaitHandle->DangerousWaitHandle().ToPointer()),
0, TIME_ONESHOT | TIME_CALLBACK_EVENT_SET);
...
}
};
-- modified at 17:31 Saturday 13th May, 2006
|
|
|
|
|
can anyone tell me how do i get the current directory in VC++ managed code?
|
|
|
|
|
String^ strCurDir = System::IO::Directory::GetCurrentDirectory();
cheers...mil10.
|
|
|
|
|
This is a C++/CLI forum. So it is asumed that all questions will be related to Managed platform. Anyway your post will be helpful for him, if he need the answer in Win32.
rgds...milton.
|
|
|
|
|
I have just recently started Computer Science as my major in college. I love playing online games and many people that I know program games online. I am using Dev-C++ as well as VB 6. My question is does anyone know where i can start on programming a videogame online.I have an encryption for a game I am curious on how to get and encryption and how to input it into text in C++ to build the game and the proxy.
Thanks
|
|
|
|
|
I am trying to use http: to retrieve a web site in html into a buffer in memory.
My code is as follows:
int Socket = clientSock(url.c_str(), 80);
if (send(Socket, (char*)str.c_str(), str.size(), 0) == SOCKET_ERROR)
{
//throw an error exception
}
Sleep(1000);
char buf[500];
int sizeOfBuf = sizeof(buf);
int bytes_read = 0;
int total_bytes_read = 0;
do
{
bytes_read = recv(Socket, buf, sizeOfBuf, 0);
myStrBuffer += RCString(buf);
total_bytes_read+=bytes_read;
memset(buf, 0, sizeof(buf));
}
while (bytes_read != 0);
where url is the host (ie "www.bbc.co.uk")
page is the relative url (ie "/sport/football")
However I cannot rerieve the information required... what i actually get is
"HTTP/1.1 400 Bad Request".
can anyone help me as to why this is happening as well as how to fix it.
Also i need to retrieve a web site with parameters
''[^]'
where 'epp.eurostat.cec.eu.int' is the host and '/portal/page' is the relative url. and everything else is a parameter list and i am unsure of how to do this.
any help would be greatly appreciated
cheers
anthony
|
|
|
|
|
Anthony Moss wrote: "HTTP/1.1 400 Bad Request".
your request was bad but you did not post your request so how can we help? Did I miss something?
led mike
|
|
|
|
|
I am building a managed C++ wrapper DLL around an unmanaged C++ DLL that uses MFC CStrings in its public interface. The unmanaged C++ DLL contains a class that I need to access. My problem is that this class contains methods using CString references, such as:
void SetName(const CString& name);
How can I call this method from my managed C++ code?
I have tried this in my wrapper code:
void CMfcDllWrapper::SetName(System::String^ str)
{
CString s(str);
m_pUnmanaged->SetName(s);
}
where m_pUnmanaged points to the C++ object in the unmanaged DLL. However, this approach causes a linker error, so I am not using the correct signature for a CString reference.
An a similar example where the CString is passed by value everything works, but with pass by reference I am stuck.
PS: I am using VS2005
Can anyone give some advise , please?
Thanks
Bo
|
|
|
|
|
CString has char* overloads. So you can try the following.
void CMfcDllWrapper::SetName(System::String^ str)<br />
{<br />
<br />
char __nogc* szStr = static_cast<char *>(Marshal::StringToHGlobalAnsi (str).ToPointer());<br />
m_pUnmanaged->SetName(szStr); <br />
Marshal::FreeHGlobal( IntPtr((void*)szStr) );<br />
<br />
}
cheers..mil10
|
|
|
|
|
can anyone tell me how do we add a dll to our project?
|
|
|
|
|
Hi,
i guess you were talking about a managed c++ dll and you would like to add it to a clr project in VS2005.
If so, then you'll have to do the following:
1. go to "Project->References", click on "Add Reference"
2. Browse to your dll and add it
3. reference the namespace of your dll
e.g.
<br />
using namespace MyDllNamespace;<br />
using namespace MyDllNamespace::Net;<br />
...<br />
regards Tobias
|
|
|
|
|
In addition to Tobias' info, you can also use #using instead of using "Add Reference" in the Project Properties:
Example:
#using <System.Data.dll>
|
|
|
|
|
Hi All
When a client using shared data from remote machine.It is very fast .But with server for same it is taking more time.
The packet transmission is 300 per share data and for server 1500 as per net stat result.
As my packet size is small i disable nagle algorithm also.but not satisfied result.So Please give some guidance.
Regards
Sabita
|
|
|
|
|
This is the first time I write a program after learning C++ for 1 week ago.
I use Dev-C++ and I want my program to do function such as:
Ask person to type two number : 1st number ; 2nd number.
such as: 2 ; 5;
and I want my program to get me the sum : 2 + 3 + 4 + 5 = 14
but I try many times and it always gives me : 2 + 5 = 7
This is my code :
using namespace std;
#include <iostream>
long result(long d , long e)
{
if (d >= 1 , d < e)
{
return (result(d + 1,0));
}
else
return (0);
}
int main()
{
long a,b;
long c;
cout << "1st number : ";
cin >> a;
cout << "2nd number : ";
cin >> b;
c = a + result(a , b) + b;
cout << c;
return 0;
}
Can someone tell what 's wrong with my code, and give me a hint to do it.
Thankz.
|
|
|
|
|
The result() function is incorrect. Try this:
long result(long d , long e)
{
long r = 0;
int i;
if (d <= e)
{
for (i == d, i <= e, i++)
{
r += i;
}
}
return (r);
} Also change this line:
c = a + result(a , b) + b;
to
c = result(a , b);
...Steve
1. quod erat demonstrandum
2. "Give a man a fish and you've fed him for a day. Teach him how to fish and you've fed him for life." I read that somewhere once
|
|
|
|
|
Thankz for your reply. This is the first time so.....I will practice more.
By the way thankz for your "fish", hope next time to can show me how to fish.
Yeah!!!
|
|
|
|