473,383 Members | 1,864 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,383 software developers and data experts.

PathYetAnotherMakeUniqueName or PathMakeUniqueName API call from C#

I'm having trouble getting the api PathYetAnotherMakeUniqueName to work.
Here is what I have so far. It is not working. What am i missing?

const int MAX_PATH = 260;

[DllImport("shell32.dll", EntryPoint="PathMakeUniqueName")]
static extern int PathMakeUniqueName (
ref string pszUniqueName,
int cchMax,
string pszTemplate,
string pszLongPlate,
string pszDir
);

[DllImport("shell32.dll", EntryPoint="PathYetAnotherMakeUniqueName")]
static extern int PathYetAnotherMakeUniqueName (
ref string pszUniqueName,
string pszPath,
string pszShort,
string pszFileSpec
);

public void Test()
{
DirectoryInfo working = new DirectoryInfo(@"..\..\");

string buffer = new string(' ', MAX_PATH);
string folderSpec = working.FullName;
string name = "New Text Document.txt";

PathYetAnotherMakeUniqueName(ref buffer, folderSpec, null, name);

}

~ Paul
Nov 16 '05 #1
14 2144
Hi Paul,
I'm having trouble getting the api PathYetAnotherMakeUniqueName to work.
Here is what I have so far. It is not working. What am i missing?
Use a StringBuilder for the first argument:
[DllImport("shell32.dll", EntryPoint="PathMakeUniqueName")]
static extern int PathMakeUniqueName (
StringBuilder pszUniqueName,
int cchMax,
string pszTemplate,
string pszLongPlate,
string pszDir
);

StringBuffer buffer = new StringBuffer(MAX_PATH);
...
PathYetAnotherMakeUniqueName(buffer, folderSpec, null, name);

bye
Rob
Nov 16 '05 #2
Hi,

Using StringBuilder allows the call to return correctly but the value is not
correct. It returns just the path and not the file name. Any ideas?

StringBuilder buffer = new StringBuilder(MAX_PATH);
PathYetAnotherMakeUniqueName(buffer, "D:\", null, "File.txt");

The value of buffer after call is just "D:\"

thanks,
~ Paul
"Robert Jordan" <ro*****@gmx.net> wrote in message
news:cl*************@news.t-online.com...
Hi Paul,
I'm having trouble getting the api PathYetAnotherMakeUniqueName to work.
Here is what I have so far. It is not working. What am i missing?


Use a StringBuilder for the first argument:
[DllImport("shell32.dll", EntryPoint="PathMakeUniqueName")]
static extern int PathMakeUniqueName (


StringBuilder pszUniqueName,
int cchMax,
string pszTemplate,
string pszLongPlate,
string pszDir
);

StringBuffer buffer = new StringBuffer(MAX_PATH);
...
PathYetAnotherMakeUniqueName(buffer, folderSpec, null, name);

bye
Rob

Nov 16 '05 #3
Hi Paul,
Using StringBuilder allows the call to return correctly but the value is not
correct. It returns just the path and not the file name. Any ideas?

StringBuilder buffer = new StringBuilder(MAX_PATH);
PathYetAnotherMakeUniqueName(buffer, "D:\", null, "File.txt");

The value of buffer after call is just "D:\"


Please post the unmanaged declaration.

bye
Rob
Nov 16 '05 #4
Here is the code i'm testing with ...

const int MAX_PATH = 260;

[DllImport("shell32.dll", EntryPoint="PathYetAnotherMakeUniqueName")]
static extern int PathYetAnotherMakeUniqueName (
StringBuilder pszUniqueName,
string pszPath,
string pszShort,
string pszFileSpec);

public void Test()
{
DirectoryInfo working = new DirectoryInfo(@"..\..\");

StringBuilder buffer = new StringBuilder(MAX_PATH);
string folderSpec = @"D:\New Text Document.txt";
string fileSpec = "New Text Document.txt";

int result = PathYetAnotherMakeUniqueName(buffer, folderSpec, null,
fileSpec);

Debug.WriteLine(buffer.ToString());
}

I've read through the documentation a little more and it said that the
filename had to be included in the path. I did so but it is still not
working. Now, it just outputs the filename again. It does not give me a
unique name.

thanks for your help,
Paul

"Robert Jordan" <ro*****@gmx.net> wrote in message
news:cl*************@news.t-online.com...
Hi Paul,
Using StringBuilder allows the call to return correctly but the value is
not correct. It returns just the path and not the file name. Any ideas?

StringBuilder buffer = new StringBuilder(MAX_PATH);
PathYetAnotherMakeUniqueName(buffer, "D:\", null, "File.txt");

The value of buffer after call is just "D:\"


Please post the unmanaged declaration.

bye
Rob

Nov 16 '05 #5
Hi Paul,
Here is the code i'm testing with ...


The unmanaged C declaration please :-))

bye
Rob
Nov 16 '05 #6
The unmaaged C declaration is ...

BOOL PathYetAnotherMakeUniqueName(
LPWSTR pszUniqueName,
LPCWSTR pszPath,
LPCWSTR pszShort,
LPCWSTR pszFileSpec
);

http://msdn.microsoft.com/library/de...uniquename.asp

thanks
Paul
"Robert Jordan" <ro*****@gmx.net> wrote in message
news:cl*************@news.t-online.com...
Hi Paul,
Here is the code i'm testing with ...


The unmanaged C declaration please :-))

bye
Rob

Nov 16 '05 #7
Hi Paul,

http://msdn.microsoft.com/library/de...uniquename.asp


It doesn't work for me either, but this is not a p/invoke or Interop
problem: it doesn't work from a C++ application too.

bye
Rob
Nov 16 '05 #8
Interesting. I wonder what trick it takes to get it to work. I've tried
PathMakeUniqueName too and it doesn't seem to work either.

thanks
Paul

"Robert Jordan" <ro*****@gmx.net> wrote in message
news:cl************@news.t-online.com...
Hi Paul,

http://msdn.microsoft.com/library/de...uniquename.asp


It doesn't work for me either, but this is not a p/invoke or Interop
problem: it doesn't work from a C++ application too.

bye
Rob

Nov 16 '05 #9
What do you mean it does not work from a c++ app? what error you get?

I made a simple c++ code, and it works for me.
///------------------------------------------------
#define UNICODE
#define _WIN32_WINNT 0x0500

#include <windows.h>
#include <shlobj.h>
#include <tchar.h>

int _tmain(int argc, _TCHAR* argv[])
{
DWORD lErr;
WCHAR oStr[MAX_PATH] = L"";
WCHAR p1[] = L"c:\\InoSetRTThread.log";
WCHAR pd[] = L"longfilename.log";

BOOL bRet = PathYetAnotherMakeUniqueName(oStr, p1,NULL, pd);

if( !bRet)
{
lErr = GetLastError();
char buf[32];
itoa(lErr,buf,16);
OutputDebugStringA(buf);
}
else
OutputDebugStringW(oStr);

return 0;
}
// eof

Best regards,
Rhett Gong [MSFT]
Microsoft Online Partner Support

This posting is provided "AS IS" with no warranties, and confers no rights.
Please reply to newsgroups only. Thanks.

Nov 16 '05 #10
Hi Rhett,
What do you mean it does not work from a c++ app? what error you get?


No errors. I simply don't understand what the function is supposed
to do. I just wanted to tell the OP there is no Interop related problem.

bye
Rob
Nov 16 '05 #11
Hi,

I was able to get the c++ version to work. I followed that exact same
syntax converted to c# and the c# version doesn't work. It simply gives
back the file name, even if it exits in the target directory.

Using c++ code provided, the output is "c:\longfilename (2).log". The
output of the c# version is "longfilename.log". There must be some Interop
issue. Anyone have any other ideas as to what to try?

thanks
Paul

"Rhett Gong [MSFT]" <v-******@online.microsoft.com> wrote in message
news:6e**************@cpmsftngxa10.phx.gbl...
What do you mean it does not work from a c++ app? what error you get?

I made a simple c++ code, and it works for me.
///------------------------------------------------
#define UNICODE
#define _WIN32_WINNT 0x0500

#include <windows.h>
#include <shlobj.h>
#include <tchar.h>

int _tmain(int argc, _TCHAR* argv[])
{
DWORD lErr;
WCHAR oStr[MAX_PATH] = L"";
WCHAR p1[] = L"c:\\InoSetRTThread.log";
WCHAR pd[] = L"longfilename.log";

BOOL bRet = PathYetAnotherMakeUniqueName(oStr, p1,NULL, pd);

if( !bRet)
{
lErr = GetLastError();
char buf[32];
itoa(lErr,buf,16);
OutputDebugStringA(buf);
}
else
OutputDebugStringW(oStr);

return 0;
}
// eof

Best regards,
Rhett Gong [MSFT]
Microsoft Online Partner Support

This posting is provided "AS IS" with no warranties, and confers no
rights.
Please reply to newsgroups only. Thanks.

Nov 16 '05 #12
The function is used to create a unique filename based on an existing
filename in the same path.
If you put a string in pszFileSpec, the output will base on the
pszFileSpec. But if you pass NULL in pszFileSpec , a unique filename is
created based on the existing filename.
eg. I put "c:\\InoSetRTThread.log" in pszPath and pass "longfilename.log"
in pszFileSpec then the output should be "longfilename.log" if there is no
file exist in the path with the same filename. Otherwise "longfilename
(2).log" will be generated.
Best regards,
Rhett Gong [MSFT]
Microsoft Online Partner Support

This posting is provided "AS IS" with no warranties, and confers no rights.
Please reply to newsgroups only. Thanks.

Nov 16 '05 #13
I was able to figure out the problem. It was an Interop issue. You simple
have to add CharSet=CharSet.Unicode to the DllImport attribute.

[DllImport("shell32.dll", EntryPoint="PathYetAnotherMakeUniqueName",
CharSet=CharSet.Unicode)]
internal static extern bool PathYetAnotherMakeUniqueName (
StringBuilder pszUniqueName,
string pszPath,
string pszShort,
string pszFileSpec );

After making this change, it works as expected.

thanks for the help,
Paul

"Paul Welter" <lo******@msdn.nospam.com> wrote in message
news:%2****************@TK2MSFTNGP14.phx.gbl...
Hi,

I was able to get the c++ version to work. I followed that exact same
syntax converted to c# and the c# version doesn't work. It simply gives
back the file name, even if it exits in the target directory.

Using c++ code provided, the output is "c:\longfilename (2).log". The
output of the c# version is "longfilename.log". There must be some
Interop issue. Anyone have any other ideas as to what to try?

thanks
Paul

"Rhett Gong [MSFT]" <v-******@online.microsoft.com> wrote in message
news:6e**************@cpmsftngxa10.phx.gbl...
What do you mean it does not work from a c++ app? what error you get?

I made a simple c++ code, and it works for me.
///------------------------------------------------
#define UNICODE
#define _WIN32_WINNT 0x0500

#include <windows.h>
#include <shlobj.h>
#include <tchar.h>

int _tmain(int argc, _TCHAR* argv[])
{
DWORD lErr;
WCHAR oStr[MAX_PATH] = L"";
WCHAR p1[] = L"c:\\InoSetRTThread.log";
WCHAR pd[] = L"longfilename.log";

BOOL bRet = PathYetAnotherMakeUniqueName(oStr, p1,NULL, pd);

if( !bRet)
{
lErr = GetLastError();
char buf[32];
itoa(lErr,buf,16);
OutputDebugStringA(buf);
}
else
OutputDebugStringW(oStr);

return 0;
}
// eof

Best regards,
Rhett Gong [MSFT]
Microsoft Online Partner Support

This posting is provided "AS IS" with no warranties, and confers no
rights.
Please reply to newsgroups only. Thanks.


Nov 16 '05 #14
Hi Paul.

It was nice to hear that you have had the problem resolved. Thanks for
sharing your experience with all the people here. If you have any
questions, please feel free to post them in the community.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."

Nov 16 '05 #15

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

Similar topics

23
by: Fabian Müller | last post by:
Hi all, my question is as follows: If have a class X and a class Y derived from X. Constructor of X is X(param1, param2) . Constructor of Y is Y(param1, ..., param4) .
35
by: hasho | last post by:
Why is "call by address" faster than "call by value"?
13
by: Bern McCarty | last post by:
I have run an experiment to try to learn some things about floating point performance in managed C++. I am using Visual Studio 2003. I was hoping to get a feel for whether or not it would make...
4
by: John | last post by:
Hi all, This really is quite an urgent matter. I have a page with multiple, dynamically-loaded user controls and when a user clicks on a button, the whole form is submitted. Now at this stage...
5
by: Amaryllis | last post by:
I'm trying to call a CL which is located on our AS400 from a Windows application. I've tried to code it in different ways, but I seem to get the same error every time. Does anyone have any clue...
13
by: mitchellpal | last post by:
i am really having a hard time trying to differentiate the two..........i mean.....anyone got a better idea how each occurs?
7
by: archana | last post by:
Hi all, I am having application in which i am doing asynchronous call.I am using manualresetevent to wait for asynchronous call to complete. I want to stop asynchronous call after certain...
13
by: shsingh | last post by:
I have a class A containing some map as data variables. I creat an object of class A on heap by allocatiing memory by using "malloc". This will return me the required memory but the object is not...
3
by: cberthu | last post by:
Hi all, Is it possible to have two connects in the same rexx script to different DB's? I have to get data form on DB (with specifics selects and filter out some values with RExx) and save the...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: 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...

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.