|
I need to write a simple C program
that prints itself.
now this is real simple only that the files name may change and the program should still work
So I need the code to find out the file's name.
Is there a simple way to access the file's name in C
Tnx a bunch
avatar
|
|
|
|
|
#include <iostream>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
wcout << argv[0] << endl;
return 0;
}
Next time, please post this type of question in the Visual C++ forum.
"We make a living by what we get, we make a life by what we give." --Winston Churchill
|
|
|
|
|
Or see
#include <iostream>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
cout << __FILE__<< endl;
return 0;
}
|
|
|
|
|
TCHAR tchFilePath_Name[200];
//Call windows API to get absolute path + file name
GetModuleFileName(GetModuleHandle(NULL), tchFilePath_Name200);
//Then pick-up the file name
//...
|
|
|
|
|
While I thought the use of "ref class" is so memory for ref intantiated objects are always allocated on managed heap. While "value class" are always allocated on stack OR un-managed heap -- however, I must be wrong, "gcnew" can allocate for "value class" as well? Getting confused...
// TestConsole.cpp : main project file.
<br />
using namespace System;<br />
<br />
value class Class1<br />
{<br />
public:<br />
Class1() {<br />
m_pUnmanagedCharString = NULL; << QUESTION 2: How can you init to NULL as you did in MFC?<br />
m_ManagedString = nullptr;<br />
}<br />
<br />
protected:<br />
char * m_pUnmanagedCharString;<br />
String^ m_ManagedString;<br />
};<br />
<br />
int main(array<System::String ^> ^args)<br />
{<br />
Console::WriteLine(L"Hello World");<br />
<br />
Class1* obj = new Class1();<br />
Class1^ obj2 = gcnew Class1(); << QUESTION 1: I thought gcnew is only for "ref classes"!?<br />
<br />
return 0;<br />
}<br />
Thanks!
http://www.codeproject.com/managedcpp/cppcliintro01.asp
http://www.codeproject.com/useritems/quickcppcli.asp
http://www.codeguru.com/cpp/cpp/cpp_managed/general/article.php/c10653/
http://www.informit.com/guides/content.aspx?g=cplusplus&seqNum=275&rl=1
|
|
|
|
|
devvvy wrote: QUESTION 2: How can you init to NULL as you did in MFC?
If your code is compiled with /clr, you can use nullptr in the initialization of native pointers, managed handles, and managed interior pointers. You can use NULL with native pointers. However, the usage of NULL can be an issue where it can be interpreted as an integer.
devvvy wrote: QUESTION 1: I thought gcnew is only for "ref classes"!?
Using gcnew on a value type will create a boxed value type that is placed on the managed heap.
"We make a living by what we get, we make a life by what we give." --Winston Churchill
|
|
|
|
|
Thanks very much but...
George L. Jackson wrote: You can use NULL with native pointers. However, the usage of NULL can be an issue where it can be interpreted as an integer.
If there's an example... please? Also I tried NULL, compiler complained that it doesnt recongnise NULL:
error C2065: 'NULL' : undeclared identifier
George L. Jackson wrote:
Using gcnew on a value type will create a boxed value type that is placed on the managed heap.
Then what's the big fuss with "ref -vs- value"? I thought the primary difference is "ref class" is always allocated on managed heap. AND "value class" is always allocated on stack OR un-managed heap (BUT NEVER on managed-heap). Sounds like they don't need "ref" keyword? Or that by virtue of being a "ref class" the object can NEVER be allocated on stack or un-managed heap?
Is that what it is?
devy
|
|
|
|
|
devvvy wrote: If there's an example... please? Also I tried NULL, compiler complained that it doesnt recongnise NULL:
NULL must be defined first: #define NULL 0 .
NULL is defined in stdio.h as zero (0).
devvvy wrote: Then what's the big fuss with "ref -vs- value"? I thought the primary difference is "ref class" is always allocated on managed heap. AND "value class" is always allocated on stack OR un-managed heap (BUT NEVER on managed-heap). Sounds like they don't need "ref" keyword? Or that by virtue of being a "ref class" the object can NEVER be allocated on stack or un-managed heap?
Is that what it is?
Value types (which includes value class ) don't use constructors for default initialization. The value type's fields are initialized to zero (or nullptr if it is a managed handle). A value type can be copied by value with less overhead than a ref class. Value types can be stored and initialized with less overhead in an array or collection than a ref class. You can copy value types using only the equal sign.
-- modified at 20:52 Sunday 11th November, 2007
"We make a living by what we get, we make a life by what we give." --Winston Churchill
|
|
|
|
|
Thanks George
George L. Jackson wrote: NULL is defined in stdio.h as zero (0).
I suppose I should readup on interop, thanks.
George L. Jackson wrote:
Value types (which includes value class) don't use constructors for default installation. The value type's fields are initialized to zero (or nullptr if it is a managed handle). A value type can be copied by value with less overhead than a ref class. Value types can be stored and initialized with less overhead in an array or collection than a ref class. You can copy value types using only the equal sign.
I thought you can do all these with "ref class" --- equal operator depends on whether you've implemented it. And all fields are initialized to either zero or nullptr - that'd be default behavior for both "ref class" and "value class"...
devy
|
|
|
|
|
Yes, you are right. However, most ref classes have more time consuming initializations and are created on the managed heap. I would use value classes rather than ref classes in a loop to lessen the amount of small objects in the manage heap. Furthermore, if you have a class that just contains value types, I would make the class a "value class".
All in all, it's your prerogative whether you use value classes or not.
"We make a living by what we get, we make a life by what we give." --Winston Churchill
|
|
|
|
|
By the way, Happy Birthday!
"We make a living by what we get, we make a life by what we give." --Winston Churchill
|
|
|
|
|
|
Hi,
I converted a sample code from C# to VC++. But I don't know how to change the "WaitCallback" statements to avoid getting error while compiling!? Here is my translated code:
Translated code to VC++
#include "stdafx.h"<br />
<br />
using namespace System;<br />
using namespace System::Threading;<br />
<br />
public ref class SomeState{<br />
public: <br />
int Cookie;<br />
public:<br />
SomeState(int iCookie)<br />
{<br />
Cookie = iCookie;<br />
}<br />
};<br />
<br />
public ref class Alpha{<br />
public:<br />
array<int>^ HashCount; <br />
ManualResetEvent^ eventX;<br />
static int iCount;<br />
<br />
static int iMaxCount;<br />
Alpha(int MaxCount) <br />
{<br />
iCount = 0;<br />
HashCount = gcnew array<int>(30);<br />
iMaxCount = MaxCount;<br />
}<br />
<br />
public:<br />
void Beta(Object^ state)<br />
{<br />
Console::WriteLine(" {0} {1} :", Thread::CurrentThread->GetHashCode(), (dynamic_cast<SomeState^>(state))->Cookie);<br />
Interlocked::Increment(HashCount[Thread::CurrentThread->GetHashCode()]);<br />
<br />
int iX = 10000;<br />
while (iX > 0){ iX--;}<br />
if (Interlocked::Increment(iCount) == iMaxCount) {<br />
Console::WriteLine("Setting EventX ");<br />
eventX->Set();<br />
}<br />
}<br />
};<br />
<br />
public class SimplePool<br />
{<br />
public:<br />
static int Main(array<System::String ^> ^args)<br />
<br />
{<br />
Console::WriteLine("Thread Simple Thread Pool Sample");<br />
int MaxCount = 1000;<br />
ManualResetEvent^ eventX = gcnew ManualResetEvent(false);<br />
Console::WriteLine("Queuing {0} items to Thread Pool", MaxCount);<br />
Alpha^ oAlpha = gcnew Alpha(MaxCount);<br />
oAlpha->eventX = eventX;<br />
Console::WriteLine("Queue to Thread Pool 0");<br />
SomeState^ sstate0=gcnew SomeState(0);<br />
ThreadPool::QueueUserWorkItem(gcnew WaitCallback(oAlpha->Beta),sstate0);<br />
for (int iItem=1;iItem < MaxCount;iItem++)<br />
{<br />
Console::WriteLine("Queue to Thread Pool {0}", iItem);<br />
SomeState^ sstatei=gcnew SomeState(iItem);<br />
ThreadPool::QueueUserWorkItem(gcnew WaitCallback(oAlpha->Beta), sstatei);<br />
}<br />
Console::WriteLine("Waiting for Thread Pool to drain");<br />
eventX->WaitOne(Timeout.Infinite,true);<br />
Console::WriteLine("Thread Pool has been drained (Event fired)");<br />
Console::WriteLine("Load across threads");<br />
for(int iIndex=0;iIndex<oAlpha->HashCount->Length;iIndex++)<br />
Console::WriteLine("{0} {1}", iIndex, oAlpha->HashCount[iIndex]);<br />
return 0;<br />
}<br />
};
(You can find the original code in the
ms-help://MS.VSCC.v80/MS.MSDN.v80/MS.NETDEV.v10.en/dndotnet/html/dotnetperftechs.htm)
Original code in C#
using System;<br />
using System.Threading;<br />
<br />
public class SomeState{<br />
public int Cookie;<br />
public SomeState(int iCookie){<br />
Cookie = iCookie;<br />
}<br />
};<br />
<br />
<br />
public class Alpha{<br />
public int [] HashCount;<br />
public ManualResetEvent eventX;<br />
public static int iCount = 0;<br />
public static int iMaxCount = 0;<br />
public Alpha(int MaxCount) {<br />
HashCount = new int[30];<br />
iMaxCount = MaxCount;<br />
}<br />
<br />
<br />
public void Beta(Object state){<br />
Console.WriteLine(" {0} {1} :", <br />
Thread.CurrentThread.GetHashCode(), ((SomeState)state).Cookie);<br />
Interlocked.Increment(ref HashCount[Thread.CurrentThread.GetHashCode()]);<br />
<br />
int iX = 10000;<br />
while (iX > 0){ iX--;}<br />
if (Interlocked.Increment(ref iCount) == iMaxCount) {<br />
Console.WriteLine("Setting EventX ");<br />
eventX.Set();<br />
}<br />
}<br />
};<br />
<br />
public class SimplePool{<br />
public static int Main(String[] args) {<br />
Console.WriteLine("Thread Simple Thread Pool Sample");<br />
int MaxCount = 1000;<br />
ManualResetEvent eventX = new ManualResetEvent(false);<br />
Console.WriteLine("Queuing {0} items to Thread Pool", MaxCount);<br />
Alpha oAlpha = new Alpha(MaxCount);<br />
oAlpha.eventX = eventX;<br />
Console.WriteLine("Queue to Thread Pool 0");<br />
ThreadPool.QueueUserWorkItem(new WaitCallback(oAlpha.Beta),new SomeState(0));<br />
for (int iItem=1;iItem < MaxCount;iItem++){<br />
Console.WriteLine("Queue to Thread Pool {0}", iItem);<br />
ThreadPool.QueueUserWorkItem(new WaitCallback(oAlpha.Beta),<br />
new SomeState(iItem));<br />
}<br />
Console.WriteLine("Waiting for Thread Pool to drain");<br />
eventX.WaitOne(Timeout.Infinite,true);<br />
Console.WriteLine("Thread Pool has been drained (Event fired)");<br />
Console.WriteLine("Load across threads");<br />
for(int iIndex=0;iIndex<OALPHA.HASHCOUNT.LENGTH;IINDEX++)<br />
Console.WriteLine("{0} {1}", iIndex, oAlpha.HashCount[iIndex]);<br />
}<br />
return 0;<br />
}<br />
}
I get these errors:
Error C3867: 'Alpha::Beta': function call missing argument list; use '&Alpha::Beta' to create a pointer to member
Error C3350: 'System::Threading::WaitCallback' : a delegate constructor expects 2 argument(s)
AlwaysStudent
|
|
|
|
|
Hi,
most problems can be solved easily by using the line numbers, either the ones mentioned
in compile errors or in Exception.ToString() output.
Advice: tell Visual Studio to always show line numbers in source files (Tools/Options/
Text Editor/All Languages...).
Luc Pattyn [Forum Guidelines] [My Articles]
this months tips:
- use PRE tags to preserve formatting when showing multi-line code snippets
- before you ask a question here, search CodeProject, then Google
|
|
|
|
|
AlwaysStudent wrote: gcnew WaitCallback(oAlpha->Beta)
Replace the above code with: gcnew WaitCallback(oAlpha, &Alpha::Beta)
"We make a living by what we get, we make a life by what we give." --Winston Churchill
|
|
|
|
|
George L. Jackson,
Thanks a lot, that is working now.
Berst Regards,
AlwaysStudent
AlwaysStudent
|
|
|
|
|
Hi,
I want to connect two combo boxes, it means that when i choose a data from the list of first combo box, the list box or edit box of second combo box change according to first combo box.
my program is on C++ language(MFC).
(i use embedded software)
please help me.
thanks a lot
|
|
|
|
|
Miss_F wrote: my program is on C++ language(MFC)
You should post this on the proper message board...
The Visual C++/MFC Board[^]
Mark
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
Next time,please post at the correct forum.
You can use of m_combo2.SetCurSel(m_Combo1.GetCurSel()); .
|
|
|
|
|
Hi,
Thanks a lot and sorry for this mistake.
|
|
|
|
|
|
Hi, I am using Visual Studio 2005 C++/CLI. I have also posted this on the Visual Studio IDE message board. I have been developing an application on my PC at my desk. This application uses the Microsoft.Office.Interop.Excel assembly that I included by adding a new reference in the project properties page by clicking the 'Add New Reference' button and selecting it from a list. I then copied the project to another machine that has Visual Studio 2005 installed on it and the compiler choked on the Excel reference. I went to add the reference to the project properties page but could not find it in the pre-defined list. I went back to my original machine and could not find Microsoft.Office.Interop.Excel in the pre-defined list either. Shouldn't this assembly be in the pre-defined list or is it defined differently? How do I get this assembly in the Visual Studio 2005 reference list? I did it once and I'm almost positive it was in the pre-defined list. What happened to it?
Thanks
Buck
|
|
|
|
|
From http://msdn2.microsoft.com/en-us/library/kh3965hw(VS.80).aspx :
During a Typical installation of Microsoft Office, the primary interop assemblies are set to be installed on first use IF the .NET Framework IS INSTALLED. This installation option works on development computers during project creation and on end user computers when an end user runs a Microsoft Office solution.
Redistributable Primary Interop Assemblies Package
The primary interop assemblies for Microsoft Office products are also available in a redistributable Microsoft Windows Installer package:
The package for Microsoft Office 2003 is available for download from the Microsoft Download Center (http://go.microsoft.com/fwlink/?LinkId=50479).
The package for the 2007 Microsoft Office system is available for download from the Microsoft Download Center (http://go.microsoft.com/fwlink/?LinkId=72637).
|
|
|
|
|
I converted a VS 6 project to VS 2005. When I execute my 2005 project none of my ON_EVENTS_ work. Any fix?
|
|
|
|
|
aschwarz wrote: I converted
what does "converted" mean? Does it mean Visual Studio prompted you to do the conversion and you clicked Yes or OK?
|
|
|
|