In Delphi, suppose I have a method with a (much simplified) signature like this:
procedure abc( const prop1:string; const arg1:TValue; const prop2:string;
out arg2:TValue );
I'm building a TList<PPropValPair> of records like this using the parameters provided:
type
TPVPType = (ptIn, ptOut);
PPropValPair = ^TPropValPair;
TPropValPair = record
io : TPVPType;
prop : string; // property name
iVal : TValue; // input value
oVar : Variant; // <-- how to save for later use??? Variant? TValue?
end;
(On the face of it, this example looks silly. Again, it's quite simplified just to communicate the problem.)
At run-time, I want to stuff all of the input values ival (where io=ptIn) into each public property 'prop' in a class, call a class method, then extract the values of all public properites 'prop' (where io=ptOut) into oVar.
The input side is working fine using RTTI.
However, I need to somehow save a REFERENCE to the output vars in oVar so I can save the value of the associated properties after the class method has been called.
I'm not assigning anything to arg2 directly. Rather, I'm saving a reference to arg2 and assigning the value indirectly later on.
The trick is ... I don't want to have to do any additional annotations of the output parameters in abc(...).
In C++, you can declare a parameter as a 'reference' by prepending it with '&'. So in C++ terms this might be defined roughly as:
procedure abc( arg1 : TValue; &arg2 : TValue );
Later, you can refer to &arg2 and it's using a POINTER to the object. But in calling the function, you just say:
abc( somevar1, somevar2 );
somevar1 is passed by value, and somevar2 is passed by reference. Inside the function, I can save somevar2 in a reference var, then later on assign a value to it via the pointer (if it's a string) by saying &arg2ref = 'abc'.
I'm guessing there's a way to do this in Delphi, either with a Variant as the oVar type, or using RTTI, or something else. I just haven't figured out the magic combination of pieces yet. (I just don't use pointers very often in Delphi.)
Maybe I need to save a raw pointer in oVar along with the type (say, oType), and cast a value through the pointer to save the property's value?
I'm hoping someone here might have some clear ideas.
BTW, I'm using Delphi XE3.
Use a pointer. It doesn't have to (and indeed shouldn't) be a "raw" pointer. Use a typed pointer, PValue. Pass in a PValue to your function, and then store that in oVal, which you should also declare a a PValue. Use # to create a pointer, and ^ to dereference.
I would not recommend passing arg2 by reference. Although you can still use # on it to get a pointer to the original variable passed to abc, the reference parameter disguises the fact that the variable needs to remain available indefinitely. Instead, declare arg2 as PValue so it's more obvious to the caller that indirection is involved.
// declaration
procedure abc(...; arg2: PValue);
// call
abc(..., #somevar2);
Related
final dList = <dynamic> [];
final List<String> sList1 = dList; // fails (can't implicitly cast)
final sList2 = dList.cast<String>(); // works (needs manual casting)
dynamic dString = '';
final String sString1 = dString; // works
final sString2 = dString as String; // works
You can see the comments in the code part what I am talking about, it is difficult to point out the piece of code here in writing part, so I added them in the code part.
List fails to convert but other types like bool, int, String works with internal casting.
The point is that dList is a List<dynamic>. The type dynamic is a top type (a supertype of all other types), and it's reified (so you can test it at run time, as opposed to Java where type arguments are erased at run time). With cast you are creating a new object, instance of List<String>, so it's allowed to be the value of a variable of that type.
With dString you already have an instance of type String (because '' evaluates to such an instance), so the cast just verifies that this is indeed a String.
You can never use a cast in Dart to obtain an object whose type is different from the starting point, it will only check the type of the existing object (and confirm that the type is as required, or throw).
I have to access several functions of a DLL written in c from Delphi (currently Delphi7).
I can do it without problems when the parameters are scalar
(thanks to the examples found in this great site!), but I have been stuck for some time when in the parameters there is a pointer to an array of Longs.
This is the definition in the header file of one of the functions:
BOOL __stdcall BdcValida (HANDLE h, LPLONG opcl);
(opcl is an array of longs)
And this is a portion of my Delphi code:
type
TListaOpciones= array of LongInt; //I tried with static array too!
Popcion = ^LongInt; //tried with integer, Cardinal, word...
var
dllFunction: function(h:tHandle; opciones:Popcion):boolean;stdcall;
arrayOPciones:TListaOpciones;
resultado:boolean;
begin
.....
I give values ββto aHandle and array arrayOPciones
.....
resultado:=dllFunction(aHandle, #arrayopciones[0]);
end;
The error message when executing it is:
"Project xxx raised too many consecutive exceptions: access violation
at 0x000 .."
What is the equivalent in Delhpi for LPLONG? Or am I calling the function in an incorrect way?
Thank you!
LONG maps to Longint, and LPLONG maps to ^Longint. So, you have translated that type correctly.
You have translated BOOL incorrectly though. It should be BOOL or LongBool in Delphi. You can use either, the former is an alias for the latter.
Your error lies in code or detail we can't see. Perhaps you didn't allocate an array. Perhaps the array is incorrectly sized. Perhaps the handle is not valid. Perhaps earlier calls to the DLL failed to check for errors.
I'm making a function in Delphi that needs a specific value as parameter, unless it is set when function is called. While te default parameter be overwritten in that case?
example:
function ExampleFunction(b = 3, a){
b*a = c
}
ExampleFunction(15,2)
Will the default parameter(3) be replaced with the given parameter(15)?
Your code does not compile. Its syntax is invalid. It looks rather as though you have written the code in some hybrid of Pascal and C#. I suggest that you fix the question.
What's more, default parameters must appear last in the list. The reason for that is that default parameters allow you to omit an parameter when calling the function. When you do that, the compiler substitutes the missing parameter with the default value. Because parameters are positional, it is not possible to omit a parameter, but then pass another parameter that appears after it in the list.
The documentation, which I urge you to read one more time, says:
Parameters with default values must occur at the end of the parameter list. That is, all parameters following the first declared default value must also have default values.
Now to the question. If you do not omit the parameter, that is if you provide it, then the value you provided is used.
Let's use an example that actually compiles:
function Test(a: Integer; b: Integer = 42): Integer;
begin
Result := a * b;
end;
Then
Test(2) = 84 // parameter b is omitted, default value passed
and
Test(4, 3) = 12
I have a group of functions that use Sub-Range types for their input parameter.
const
ImprovementNodeCount = 20;
SaleAllocationNodeCount = 10;
type
TImprovementNodePrintOrders = 0..ImprovementNodeCount;
TSaleAllocationNodePrintOrders = 0..SaleAllocationNodeCount;
function SaleImprovementType(PrintOrder: TImprovementNodePrintOrders): TSaleReferenceRecord;
function SaleAllocationType(PrintOrder: TSaleAllocationNodePrintOrders): TSaleReferenceRecord;
function SaleAllocationAcres(PrintOrder: TSaleAllocationNodePrintOrders): TSaleReferenceRecord;
// many more functions with different SubTypes
This has been working very well for me. I have a new situation where it would be convenient to pass one of these functions as a parameter. My sub-ranges are now causing problems because they are different types.
All of the functions look the same except for the sub-type. I tried to add a new function type like this
TGetReferenceFunction = function (Index: cardinal): TSaleReferenceRecord;
Right now the compiler complains that the types are different when I try to pass the function parameter as a TGetReferenceFunction. Is there any way to create a function type that will include all of these functions that have different sub-type parameters?
[DCC Error] SaleNameMap.pas(295): E2010 Incompatible types: 'Cardinal' and 'TImprovementNodePrintOrders'
What I really need is a type that is all numeric sub-types. I know I can create a different function type for each sub-type I have, but that still will not let me pass these functions as parameters into one common function.
I'm guessing this is not possible. If so I have some other options, but in case there is something I am missing I thought I would try here first.
If you really want one function to accept all those distinct function types, then you'll have to sacrifice type safety. You can do this with a cast:
TGetReferenceFunction(#SaleAllocationAcres)
You need to simulate a kind of anonymous method system.
For example:
// existing typed proc
function SaleAllocationType(PrintOrder: TSaleAllocationNodePrintOrders): TSaleReferenceRecord;
function SaleAllocationAcres(PrintOrder: TSaleAllocationNodePrintOrders): TSaleReferenceRecord
Type
// the prototype
SaleAllocProto = function(PrintOrder: TSaleAllocationNodePrintOrders): TSaleReferenceRecord;
// enumeration for all the existing typed procs
TSelAllocRef = (saType,saAcres);
Const
// array which references all your typed procs.
SelAllocProvider = Array[TSelAllocRef] Of TSelAllocRef = (SaleAllocationType,SaleAllocationAcres);
Then you can call
ASaleReferenceRecord := SelAllocProvider[saType](Myargument);
AnotherSaleReferenceRecord := SelAllocProvider[saAcres](Myargument)
I tried to make my code as simple as possible,but I failed at it.
This is my code:
class function TWS.WinsockSend(s:integer;buffer:pointer;size:word):boolean;
begin
dwError := Send(s,buffer,size,0);
// Debug
if(dwError = SOCKET_ERROR) then
begin
dwError := WSAGetLastError;
CloseSocket(s);
WSACleanup;
case (dwerror) of
//Case statement
else
LogToFile('Unhandled error: ' + IntToStr(dwError) + ' generated by WSASend');
end;
Exit(false);
end;
// if the size of the bytes sent isn't the expected one.
while(dwError <> size) do
dwError:= dwError + Send(s,Ptr(cardinal(buffer) + dwError),size-dwError,0);
Exit(true);
end;
The error is placed at
dwError:= dwError + Send(s,Ptr(cardinal(buffer) + dwError),size-dwError,0);
Error is "Constant object cannot be passed as var parameter"
I understand I need a variable,but isn't there a way I can do it without adding one more line?
When the compiler complains about the way you're passing a parameter, the first thing you need to know is what the parameter expects. Therefore, you should go look at the declaration of Send. If looking at the declaration doesn't immediately give you an idea of what to fix, then you need to include that declaration with the code you post in your question.
I suspect that this actually has nothing to do with incrementing a pointer. Instead, the compiler is complaining about the third parameter, where you are trying to pass the expression size-dwError. I guess the parameter is declared like this:
var buffersize: Word;
The function plans on providing a new value for that parameter β that's what var means β so the thing you pass to that parameter needs to be something that can receive a value. You can't assign a new value to the result of subtracting two variables.
Take a closer look at where the compiler complained about that line. Didn't it place the cursor somewhere near the third parameter? That's a clue that the problem is there.
Decrement size, and then pass it to the function.
Dec(size, dwError);
Inc(dwError, Send(s, Ptr(cardinal(buffer) + dwError), size, 0));
Why do you care about adding another line? Have you reached your quota for the day? Lines are cheap; don't be afraid to use two to express yourself when one won't do. Likewise for variables. When your code doesn't work, saving a byte or two doesn't matter at all.
At the very least, you should have added more lines in order to track down the source of the problem. When you have one line of code that's performing several independent calculations (such as getting a new pointer value, getting a new size, and calling a function), break the line into several separate pieces. That way, if there's a problem with one of them and the compiler complains, you'll know exactly which one to blame.
Correct, this will not work as written. When your dealing with var parameters, you have to build the parameter BEFORE passing it to the procedure/function. When a Var parameter is passed, the procedure is allowed to modify it. If you attempted to copy two variables together on the call, where would this result go?
The other issue is that dwError is not delcared. A class method does NOT have access to the data elements of the object the class defines. If you drop the class, then you will have access to the data elements, but will require that the class first be created.
You should only be using class methods in places where the input and output are completely contained within the method.
How are you allocating your buffer? Internally is it an array?
Sounds like Send has a format parameter (like send (const something;size:integer)
Workaround is using pchar (entirepointerexpression)[0]